手冊上解釋:
CURLOPT_POSTFIELDS
全部數據使用HTTP協議中的"POST"操作來發送。要發送文件,在文件名前面加上@前綴並使用完整路徑。這個參數可以通過urlencoded后的字符串類似'para1=val1¶2=val2&...'或使用一個以字段名為鍵值,字段數據為值的數組。如果value是一個數組,Content-Type頭將會設置成multipart/form-data。
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。這個是正常的網頁<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。雖然對於大多數web服務器並沒有影響,但是還是有少部分服務器不兼容。
本文得出的結論是,在沒有需要上傳文件的情況下,盡量對post提交的數據進行http_build_query,然后發送出去,能實現更好的兼容性,更小的請求數據包。
備注:
string http_build_query ( mixed $query_data
[, string $numeric_prefix
[, string $arg_separator
[, int $enc_type
= PHP_QUERY_RFC1738
]]] )
使用給出的關聯(或下標)數組生成一個經過URL-encode 的請求字符串。
curl發送post數據的時候,如果用http_build_query生成對應的post數據,則不用urlencode編碼對應的字符串了
如果是手動拼接,則需要對中文手動用urlencode編碼