White spaces in postFields in PHP Curl

◇◆丶佛笑我妖孽 提交于 2019-12-13 07:03:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!