PHP中CUR的CURLOPT_POSTFIELDS请求时,Content-Type多出boundary=------------------------

当我们采用 CURL 在不注意细节的前提下向服务器发送一些数据,得到请求头的CONTENT_TYPE:

[CONTENT_TYPE] => multipart/form-data; boundary=—————————-f924413ea122



后面多出一个boundary,导致服务端获取不到提交的参数。

解决办法:

curl_setopt ( $curl, CURLOPT_POST, 1);
curl_setopt ( $curl, CURLOPT_HTTPHEADER,array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt ( $curl, CURLOPT_POSTFIELDS,http_build_query($params));

http_build_query($post_data) 来替代 $post_data 再向这个 PHP 脚本提交数据的时候,我们就会得到和上面不同的结果,这才是我们理想中的结果:



原因分析:

从上面这个例子中不难看出,使用 CURL 并且参数为数据时,向服务器提交数据的时候,HTTP头会发送Content_type: application/x-www-form-urlencoded。这个是正常的网页<form>提交表单时,浏览器发送的头部。而 multipart/form-data 我们知道这是用于上传文件的表单。包括了 boundary 分界符,会多出很多字节。
官方的手册上是这样说的:

The full data to post in a HTTP “POST” operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like ‘para1=val1¶2=val2&…' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.


使用数组提供 post 数据时,CURL 组件大概是为了兼容 @filename 这种上传文件的写法,默认把 content_type 设为了 multipart/form-data。虽然对于大多数服务器并没有影响,但是还是有少部分服务器不兼容。


经过一番总结最终得出结论:在没有需要上传文件的情况下,尽量对 post 提交的数据进行 http_build_query 处理,然后再发送出去,能实现更好的兼容性,更小的请求数据包。


猜你喜欢

转载自blog.csdn.net/meimeieee/article/details/80146660