PHP中CURL的CURLOPT-POSTFIELDS参数使用细节
博学站
人气:2.27W
CURL确实是一个不错的好工具,不仅在PHP中还是其他的操作系统中,都是一个非常好用的。但是如果你有些参数没有用好的话,那可能会得不到自己理想中的结果。具体情况如下,更多消息请关注应届毕业生网! 在通常情况下,我们使用 CURL 来提交 POST 数据的时候,我们已经习惯了这样的'写法: 代码如下: curl_setopt( $ch, CURLOPT_POSTFIELDS,$post_data);但是这样的写法在有时候并不会很好用,可能会得到服务器返回的 500 错误。但是我们尝试在使用 Socket 方式向服务器提交数据的时候,我们会得到非常正确的结果。 例如我们在服务器上面存在一个如下的 PHP 文件: 代码如下: <?php print_r($_SERVER);?> 当我们采用 CURL 在不注意细节的前提下向服务器发送一些数据,我们可能得到下面这样的结果,这不是我们理想中的结果: 代码如下: [CONTENT_TYPE] => multipart/form-data; boundary=—————————-f924413ea122但是如果我们在采用 http_build_query($post_data) 来替代 $post_data 再向这个 PHP 脚本提交数据的时候,我们就会得到和上面不同的结果,这才是我们理想中的结果: 代码如下: [CONTENT_TYPE] => application/x-www-form-urlencoded从上面这个例子中不难看出,使用 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 处理,然后再发送出去,能实现更好的兼容性,更小的请求数据包。
相关文章:
- 人教PEP版小学五年级英语下册Unit3Myschoolcalender.ALet’stalk
- Zend studio for eclipse中使php可以调用mysql相关函数的设置方
- 如何在PHP中处理Protocol Buffers数据
- Efforts Urged to Improve Womens Reproductive Health英语阅读
- 少儿英语小故事:She Practices Flips for the Olympic
- 英语作文Let the Surprise Speak of Your Love
- php内核分析之sapi-module-struct
- 英语短文Do Old Folks Still Appreciate Humor?
- PHP中使用cURL实现Get和Post请求
- 如何在PHP中处理Protocol Buffers数据大纲
热门推荐
- 1php中file-get-contents与curl性能比较
- 2英语阅读:Are You Tricked by These Optical Illusions
- 3php中file-get-contents与curl性能分析
- 4Jolie drops battle over perfume named Shilohy英语作文
- 5高中英语作文:My approach to difficulties in learning
- 6php中file-get-contents与curl性能比较分析
- 7php中file-get-contents与curl性能比较分析的内容
- 8PHP中使用cURL实现Get和Post请求的方法
- 9初三英语《The worlds population Lesson》教案设计
- 10php的file-get-contents与curl性能分析
猜你喜欢
- 1六下英语Unit5Ourearthlookslikethisfromspace教案
- 2浅谈php中curl和fsockopen的应用
- 3php中allow-url-include的应用分析
- 4PHP如何使用curl发送GET和POST请求
- 5英语阅读推荐:How to protect cultural identity
- 6七年级英语Unit2Is this your pencil Period1说课稿
- 7Relieve Psychological Pressure的英语短文
- 8英语美文:Olympic Fund Scratchcard Launched
- 9Thinkpad笔记本开机显示Tointerrruptnormalstartup解决方法
- 10大学生面临许多压力The Pressures on Collese Students英语作文
- 11The Unforgettable School Trip英语作文
- 12英语翻译the world‘s population
- 13Pick the Worlds Ugliest Insect英语作文
- 14The world Cup Football Final英语作文
- 15Unit 7 After school Period 2
最近更新
- 1The Risks of Plastic Surgery高中英语作文及翻译
- 2专业面试过程细节中的细节
- 3劳动使我重视细节
- 4PHP如何用curl发送GET和POST请求
- 5PHP中curl的使用实例
- 6关于Applying For A Student Discount Card的留学英语口语
- 7英语阅读:Dollar Hit by Trump Tax Plan Doubts
- 8英语短文:City Life Compared with Rural Life
- 9三年级上册Unit2 Colours Part B Let’s talk教学反思
- 10数学错题本的使用
- 11英语阅读:VIT declines to acquire Ang Mo Kio property
- 12Choice of large enterprises or small英语四级作文
- 13英语作文展示:all those friendly people
- 14Excel中Sumproduct函数的使用方法
- 15Stop Polluting the Water高中英语作文
- 16用Photoshop如何绘制Google Currents图标
- 17PEP六年级上册《Unit1Howdoyougotoschool》知识点归纳
- 18Ought Parents to Give Children Pocket英语作文
- 19初中英语《Unit 1 Will people have robots》教学设计