PHP curl 的CURLOPT_POSTFIELDS之數組和字串之謎____PHP

來源:互聯網
上載者:User
現象

在最近的工作中遇到一個問題,就是使用post發送請求,post資料死活傳遞過不去,一直是請求返回error。
代碼如下:

$post = array(    'userid' => 1000034443,);function curlPost($url, $headers, $post){    $ch = curl_init();    curl_setopt($ch,CURLOPT_URL,$url);    if(!empty($headers)){        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);    }    curl_setopt($ch, CURLOPT_POST, 1);//設定為POST方式    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//POST資料    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //如果成功只將結果返回,不自動輸出任何內容。    curl_setopt($ch, CURLOPT_HEADER,0);//如果想把一個頭包含在輸出中,設定這個選項為一個非零值。    curl_setopt($ch, CURLINFO_HEADER_OUT,1);//啟用時追蹤控制代碼的請求字串。    $json = curl_exec($ch);    $headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);    echo "\n\n=====請求返回=====\n";    echo "out headers:\t".$headers ."\n";    $hearLen = curl_getinfo($ch, CURLINFO_HEADER_SIZE);    echo "header len:\t".$hearLen ."\n";    $statuscode = curl_getinfo($ch, CURLINFO_HTTP_CODE);    echo "httpcode:\t".$statuscode."\n";    echo "\n===================\n";    return $json;    @curl_close($ch);}

發現什麼問題嗎。
結果如下:

感覺沒什麼問題啊。怎麼會傳不過去呢。然後就憑著感覺修改了代碼

function curlPost($url, $headers, $post){    $ch = curl_init();    curl_setopt($ch,CURLOPT_URL,$url);    if(!empty($headers)){        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);    }    curl_setopt($ch, CURLOPT_POST, 1);//設定為POST方式    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));//POST資料    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //如果成功只將結果返回,不自動輸出任何內容。    curl_setopt($ch, CURLOPT_HEADER,0);//如果想把一個頭包含在輸出中,設定這個選項為一個非零值。    curl_setopt($ch, CURLINFO_HEADER_OUT,1);//啟用時追蹤控制代碼的請求字串。    $json = curl_exec($ch);    $headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);    echo "\n\n=====請求返回=====\n";    echo "out headers:\t".$headers ."\n";    $hearLen = curl_getinfo($ch, CURLINFO_HEADER_SIZE);    echo "header len:\t".$hearLen ."\n";    $statuscode = curl_getinfo($ch, CURLINFO_HTTP_CODE);    echo "httpcode:\t".$statuscode."\n";    echo "\n===================\n";    return $json;    @curl_close($ch);}

哪裡不一樣了呢。
看看效果先:

對比發現,content type值不一樣了。。。

怎麼回事呢。

當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&para2=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資料時,預設把content_type設為了multipart/form-data,雖然對於大多數web伺服器並沒有影響,但是還是有少部分伺服器不相容. 結論

這次我的沒有返回multipart/form-data,但確實是這個問題,建議post資料可以使用http_build_query()函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.