0

我有下一个代码:

$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://www.mydomain.com/test.php');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS,"First Name=Jhon&Last Name=bt");
curl_exec ($c);
curl_close ($c);

我的问题是这段代码中的空格:

"First Name=Jhon&Last Name=bt"

我尝试了下一个代码:

$test=rawurlencode("First Name=Jhon&Last Name=bt");

$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://www.mydomain.com/test.php');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS,$test);
curl_exec ($c);
curl_close ($c);

我也试过

$first_name=rawurlencode("名字");

$last_name=rawurlencode("姓氏");

$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://www.mydomain.com/test.php');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS,"$first_name=Jhon&$last_name=bt");
curl_exec ($c);
curl_close ($c);

我的错误是什么?不管用。我需要使用变量“名字”和“姓氏”的名称发送。谢谢你的帮助。

4

1 回答 1

1

独立的 curl 编码。

http://curl.haxx.se/docs/manpage.html#--data-urlencode

PHP urlencoding

只需先对值进行编码,然后将其作为 post 字段选项传递给 curl。

$encodedValue = urlEncode("this has spaces - oh no!");
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://www.mydomain.com/test.php');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS , "value=" . $encodedValue);
curl_exec ($c);
curl_close ($c);

或者,如果您的钥匙有空格:

 $encodedKey = urlEncode("first name");
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, 'http://www.mydomain.com/test.php');
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS , $encodedKey . "=Bobby");
    curl_exec ($c);
    curl_close ($c)
于 2012-04-20T13:02:36.700 回答