使用 cURL 编码的 POST url
当我必须使用 CURL 发送 POST 请求时,在 POST 字段中,如果它们包含带空格的字符串,我是否必须对字符串进行 url_encode,或者该函数会为我执行此操作?
什么是正确的?
CURLOPT_POSTFIELDS => 'field=this%20is%20good'
或者
CURLOPT_POSTFIELDS => 'field=this is good'
我问这个是因为我不希望它对字符串进行两次编码从而发送错误的数据。
谢谢
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 thus send incorrect data.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你没有指定,但我想你使用的是 PHP,对吗?
它必须根据 RFC 1738 进行 urlencoded。
您可以使用函数http_build_query()。例如,我使用:
您也可以简单地传递 $array 作为选项(如
CURLOPT_POSTFIELDS => $array
),但这将创建一个 multipart/form-data 请求,而不是“正常” “应用程序/x-www-form-urlencoded。PS:实际上,你的都不是正确的:)你应该根据 RFC 1738 用 + 编码空格,而不是用 %20 编码。
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:
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.