我有下一个代码:
$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”的名称发送。谢谢你的帮助。
答案 0 :(得分:1)
http://curl.haxx.se/docs/manpage.html#--data-urlencode
首先对值进行编码,然后传递给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)