问题
I have the next code:
$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"
I tried the next code:
$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);
Also I tried
$first_name=rawurlencode("First 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);
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.
回答1:
standalone curl ENcoding.
http://curl.haxx.se/docs/manpage.html#--data-urlencode
PHP urlencoding
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)
来源:https://stackoverflow.com/questions/10246734/white-spaces-in-postfields-in-php-curl