PHP Curl中postFields中的空格

时间:2012-04-20 12:59:54

标签: php curl libcurl

我有下一个代码:

$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(“名字= Jhon&姓氏= 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);

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

1 个答案:

答案 0 :(得分:1)

独立卷曲编码。

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

PHP urlencoding

首先对值进行编码,然后传递给curl作为post field选项。

$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)