PHP的CURL組件是非常常用的HTTP請求模擬器。 通常要發(fā)送post數(shù)據(jù)時,我已經(jīng)習慣于這樣寫: curl_setopt( $ch, CURLOPT_POSTFIELDS,$post_data); 但是在向某一個服務器發(fā)送請求的時候,服務器返回500。而使用socket方式連接上去發(fā)送請求,返回正常的數(shù)據(jù)。 嘗試發(fā)送到一個僅有<?php print_r($_SERVER);?>的網(wǎng)頁上,可以看到使用數(shù)組發(fā)送POST數(shù)據(jù)時,收到的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被設置為數(shù)組時,HTTP頭會發(fā)送Content_type: application/x-www-form-urlencoded。這個是正常的網(wǎng)頁<form>提交表單時,瀏覽器發(fā)送的頭部。而multipart/form-data我們知道這是用于上傳文件的表單。包括了boundary分界符,會多出很多字節(jié)。 手冊上提到: 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.
使用數(shù)組提供post數(shù)據(jù)時,CURL組件大概是為了兼容@filename這種上傳文件的寫法,默認把content_type設為了multipart/form-data。雖然對于大多數(shù)web服務器并沒有影響,但是還是有少部分服務器不兼容。 本文得出的結論是,在沒有需要上傳文件的情況下,盡量對post提交的數(shù)據(jù)進行http_build_query,然后發(fā)送出去,能實現(xiàn)更好的兼容性,更小的請求數(shù)據(jù)包。
|