PHP Curl中postFields中的空白 [英] White spaces in postFields in PHP Curl
问题描述
我有下一个代码:
$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);
我的问题是这段代码中的空格:
My problem is with the spaces in this code:
"First Name=Jhon&Last Name=bt"
我尝试了下一个代码:
$ test = rawurlencode(名字= 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(名字);
$first_name=rawurlencode("First Name");
$ last_name = rawurlencode( Last Name);
$last_name=rawurlencode("Last 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,"$first_name=Jhon&$last_name=bt");
curl_exec ($c);
curl_close ($c);
我的错误是什么?不管用。我需要发送变量名 First Name和 Last Name。谢谢您的帮助。
What is my error? is not working. I need to send with the name of variables "First Name" and "Last Name". Thanks for your help.
推荐答案
独立的curl编码。
< a href = http://curl.haxx.se/docs/manpage.html#--data-urlencode rel = nofollow> http://curl.haxx.se/docs/manpage.html#-- data-urlencode
只需先对值进行编码,然后传递给curl
Just encode the values first, then pass to curl as post field options.
$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);
或者,如果您的键带有空格:
OR, if your key has the spaces:
$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)
这篇关于PHP Curl中postFields中的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!