开发者

POST url enconding with cURL

开发者 https://www.devze.com 2023-04-01 05:03 出处:网络
when I have to send a POST request with CURL, in the POST fields if they contain strings with spaces do I have to url_encode the strings or the function will do it for me?

when I have to send a POST request with CURL, in the POST fields if they contain strings with spaces do I have to url_encode the strings or the function will do it for me?

what's correct?

CURLOPT_POSTFIELDS  => 'field=this%20is%20good'

or this

CURLOPT_POSTFIELDS  => 'field=this is good'

I'm asking because I don't want it to encode the string twice and th开发者_如何学JAVAus send incorrect data.

Thank you


You did not specify that, but I suppose you're using PHP, right?

It must be urlencoded according to RFC 1738. You can use the function http_build_query(). I use, for example:

$array = array('field' => 'this is good');
http_build_query($array, '', '&');

You could also simply pass $array as the option (like CURLOPT_POSTFIELDS => $array), but this will create a multipart/form-data request, instead of the "normal" application/x-www-form-urlencoded.

PS: None of yours are correct, actually :) You should encode spaces with +, as per the RFC 1738, and not with %20.

0

精彩评论

暂无评论...
验证码 换一张
取 消