php curlopt_postfields,PHP的CURLOPT_POSTFIELDS参数使用数组和字符串的区别 - CSDN博客
PHP的CURL組件是非常常用的HTTP請求模擬器。
通常要發送post數據時,我已經習慣于這樣寫:
curl_setopt( $ch, CURLOPT_POSTFIELDS,$post_data);
但是在向某一個服務器發送請求的時候,服務器返回500。而使用socket方式連接上去發送請求,返回正常的數據。
嘗試發送到一個僅有<?php print_r($_SERVER);?>的網頁上,可以看到使用數組發送POST數據時,收到的CONTENT_TYPE如下:
[CONTENT_TYPE] => multipart/form-data; boundary=—————————-f924413ea122
而使用http_build_query($post_data),收到的CONTENT_TYPE如下:
[CONTENT_TYPE] => application/x-www-form-urlencoded
可見,當CURLOPT_POSTFIELDS被設置為數組時,HTTP頭會發送Content_type: application/x-www-form-urlencoded。這個是正常的網頁
提交表單時,瀏覽器發送的頭部。而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。雖然對于大多數web服務器并沒有影響,但是還是有少部分服務器不兼容。
本文得出的結論是,在沒有需要上傳文件的情況下,盡量對post提交的數據進行http_build_query,然后發送出去,能實現更好的兼容性,更小的請求數據包。
總結
以上是生活随笔為你收集整理的php curlopt_postfields,PHP的CURLOPT_POSTFIELDS参数使用数组和字符串的区别 - CSDN博客的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 烟火人家取名寓意
- 下一篇: 电费太高,这几个省电妙招送给你?