如何使用 CURL 发送数组? 我应该对其进行“urlencode”吗?
当我分配要作为 cURL 选项发布的数据数组(通过 CURLOPT_POSTFIELDS)时,我是否需要先对该数据进行 urlencode 还是会处理它?
When I assign an array of data to be POSTed as a cURL option (via CURLOPT_POSTFIELDS), do I need to urlencode that data first or will that be taken care of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
curl_setopt
的 C 实现似乎没有对文本进行 URL 编码。 但是,在 PHP5 中,http_build_query
函数返回以下内容的查询字符串表示形式: URL 编码的数组。用法示例
The C implementation of
curl_setopt
doesn't seem to URL-encode the text. However, in PHP5, thehttp_build_query
function returns a query string representation of the array that is URL-encoded.Example Usage
您不必先进行 urlencode。 但是,重要的是要认识到传递数组将使 cURL 将其作为
multipart/form-data
发送,这解释了为什么它不需要进行 urlencoded(无论是您还是 cURL),并且如果要上传文件,则需要使用数组。 如果您首先http_build_query()
(并将其作为字符串发送),它将被视为application/x-www-form-urlencoded
。You don't have to urlencode first. However, it is important to realize that passing an array will make cURL send it as
multipart/form-data
, which explains why it is does not need to get urlencoded (by neither you nor cURL), and you need to use an array if you want to upload files. If youhttp_build_query()
first (and send it as a string) it will be treated asapplication/x-www-form-urlencoded
.将数组用于 CURLOPT_POSTFIELDS 的一个问题是,不能拥有带有空值的名称-值对。
One problem with using an array for CURLOPT_POSTFIELDS is that you can't have a name-value pair with an empty value.
我使用:
而不是:
I use:
instead of:
POST 数据不会添加到 URL(如 GET),因此您无需对其进行 URL 编码。
POST data is not added to the URL (like GET) so you don't need to URLencode it.