如何在curl php中传递url值

发布于 2024-07-28 23:32:04 字数 807 浏览 2 评论 0原文

$description = "some test data and url";
$description .="http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow";

curl_setopt($sch, CURLOPT_URL, "myserverurl");
curl_setopt($sch, CURLOPT_HEADER, 0);             
curl_setopt($sch, CURLOPT_POST, true);
curl_setopt($sch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($sch, CURLOPT_POSTFIELDS, "orgid=$orgid&description=$description&external=1");
curl_setopt ($sch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($sch, CURLOPT_SSL_VERIFYPEER, 0); 

当我检查服务器(myserverurl)时。

我可以看到描述字段,例如

“一些测试数据和网址 http://www.mydata.com?test =1”。

我丢失了“&”之后的描述

是的,我们可以在使用curl发送之前对url进行编码,但我无权在第三方api服务器上再次解码url

$description = "some test data and url";
$description .="http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow";

curl_setopt($sch, CURLOPT_URL, "myserverurl");
curl_setopt($sch, CURLOPT_HEADER, 0);             
curl_setopt($sch, CURLOPT_POST, true);
curl_setopt($sch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($sch, CURLOPT_POSTFIELDS, "orgid=$orgid&description=$description&external=1");
curl_setopt ($sch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($sch, CURLOPT_SSL_VERIFYPEER, 0); 

when i check on the server (myserverurl).

I can see the description field like

"some test data and url http://www.mydata.com?test=1".

i lost the description after '&'

yes , we can encode the url before sending with curl, but i do not have access to decode the url again on that third party api server

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蹲在坟头点根烟 2024-08-04 23:32:04

如果您urlencode每个参数的会怎么样?正在发送?

您不必担心另一端的解码:这是通过 GET / POST 发送数据的标准方式

curl_setopt($sch, CURLOPT_POSTFIELDS, 
    "orgid=" . urlencode($orgid) 
    . "&description=" . urlencode($description) 
    . "&external=1"
);

如果这不起作用,请尝试使用 rawurlencode ? (如果我没记错的话,空格是有区别的)

What if you urlencode the value of each parameter you are sending ?

You don't have to worry about decoding on the other side : it is standard way of sending data through GET / POST

Something like :

curl_setopt($sch, CURLOPT_POSTFIELDS, 
    "orgid=" . urlencode($orgid) 
    . "&description=" . urlencode($description) 
    . "&external=1"
);

And if this doesn't work, try with rawurlencode ? (there is a difference for spaces, if I remember correctly)

桃扇骨 2024-08-04 23:32:04

简单的。 输入后即可获取当前 URL。

因此,如果您输入

http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow

(我假设 mydata.com 是您的服务器),您可以非常轻松地回显它:

$url = str_replace("?","",$_SERVER['REQUEST_URI']);
echo $url;

并且上面的回显应该给出:

test=1&user=4&destination=645&source=stackoverflow

从那时起,您可以简单地将整个字符串保存到数据库,或通过 $_SERVER['test'] 保存各个变量(测试、用户、目标、源),或者,如果其中一些变量不同,您可以通过 分解它们& 字符来动态保存它们。

Easy. You can simply get the current URL after it has been entered.

So if you enter

http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow

(I assume, that mydata.com is your server), you can echo it very easily:

$url = str_replace("?","",$_SERVER['REQUEST_URI']);
echo $url;

And the above echo should give:

test=1&user=4&destination=645&source=stackoverflow

From then, you can simply either save the entire string to the database, or save individual variables (test, user, destination, source) by either $_SERVER['test'], or, if a number of them varies, you can explode them by & character to save them dynamically.

乖乖兔^ω^ 2024-08-04 23:32:04

将发布的数据放入一个数组中,并使用该数组作为您的 CURLOPT_POSTFIELDS
您可以在$_POST['description']中获取描述。

示例
test.php

<?php
$sch = curl_init(); 
$post_data=array();
$orgid='testorg';
$description = "some test data and url";
$description .="http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow";
$post_data['description']=$description;
$post_data['orgid']=$orgid;
$post_data['external']=1;
curl_setopt($sch, CURLOPT_URL, "localhost/testcurl.php");
curl_setopt($sch, CURLOPT_HEADER, 0);             
curl_setopt($sch, CURLOPT_POST, true);
curl_setopt($sch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($sch, CURLOPT_POSTFIELDS, $post_data);
$re=curl_exec($sch);

echo('<pre>');
echo($re);

testcurl.php

<?php
var_dump($_POST);

结果

array(3) {
  ["description"]=>
  string(94) "some test data and urlhttp://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow"
  ["orgid"]=>
  string(7) "testorg"
  ["external"]=>
  string(1) "1"
}

Put the data for post in an array and use that array as your CURLOPT_POSTFIELDS.
You can get the description in $_POST['description'].

Example
test.php

<?php
$sch = curl_init(); 
$post_data=array();
$orgid='testorg';
$description = "some test data and url";
$description .="http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow";
$post_data['description']=$description;
$post_data['orgid']=$orgid;
$post_data['external']=1;
curl_setopt($sch, CURLOPT_URL, "localhost/testcurl.php");
curl_setopt($sch, CURLOPT_HEADER, 0);             
curl_setopt($sch, CURLOPT_POST, true);
curl_setopt($sch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($sch, CURLOPT_POSTFIELDS, $post_data);
$re=curl_exec($sch);

echo('<pre>');
echo($re);

testcurl.php

<?php
var_dump($_POST);

result

array(3) {
  ["description"]=>
  string(94) "some test data and urlhttp://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow"
  ["orgid"]=>
  string(7) "testorg"
  ["external"]=>
  string(1) "1"
}
神也荒唐 2024-08-04 23:32:04

这是简单的解决方案。 如果您正在使用 PHP 函数curl_escape()`

$msg=$_POST['Message'];

$ch = curl_init();
$new = curl_escape($ch, $msg);


$ch = curl_init("url".$new."/xyz.com");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);

This is the easy solution. If you are working on php use function curl_escape()`

$msg=$_POST['Message'];

$ch = curl_init();
$new = curl_escape($ch, $msg);


$ch = curl_init("url".$new."/xyz.com");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文