PHP提供的curl是非常常用的功能,估計很多小伙伴還沒有研究透HTTP協議就可以傻瓜式的使用了,今天不會學習HTTP的知識,而是要分享一個關於curl開發的細節,就是HTTP請求頭協議中的 Content-Type 字段在PHP的curl中的使用。我會在另一篇文章中分享HTTP協議相關知識。
我們在使用PHP進行開發過程中,會經常對接別人的接口,尤其是當下互聯互通的大趨勢下,使用curl的場景會更多,有些細節是需要特別注意的。
今天要講的HTTP請求頭的Content-Type字段,就是在curl發送請求的時候需要指定以何種方式來請求數據,按照postman的POST方法的分類,常用的有3類:
1、form-data 就是 multipart/form-data 使用表單的方式來發送數據 是curl采用的默認發送方式。
2、x-www-form-urlencoded 就是 application/x-www-form-urlencoded 把請求的內容轉變成url參數的形式進行發送,如:s1=red&s2=blue,這是標准的編碼格式,但在curl中卻不是默認的發送方式。
3、raw(text/plain application/json text/xml等) 是以純文本的方式發送數據,可以選擇json、xml等格式
舉個用postman以form-data發送的例子來說明:

form-data的方式是以boundary為分隔符,表單中每個鍵值對字段是以Content-Disposition來分割。
1 <?php 2 3 $curl = curl_init(); 4 5 curl_setopt_array($curl, array( 6 CURLOPT_URL => "http://www.baidu.com", 7 CURLOPT_RETURNTRANSFER => true, 8 CURLOPT_ENCODING => "", 9 CURLOPT_MAXREDIRS => 10, 10 CURLOPT_TIMEOUT => 30, 11 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 12 CURLOPT_CUSTOMREQUEST => "POST", 13 CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"s1\"\r\n\r\nred\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"s2\"\r\n\r\nblue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", 14 CURLOPT_HTTPHEADER => array( 15 "cache-control: no-cache", 16 "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" 17 ), 18 )); 19 20 $response = curl_exec($curl); 21 $err = curl_error($curl); 22 23 curl_close($curl); 24 25 if ($err) { 26 echo "cURL Error #:" . $err; 27 } else { 28 echo $response; 29 }
用postman以x-www-form-urlencoded發送的例子:

PHP的curl默認是采用form-data方式,所以要把選項CURLOPT_POSTFIELDS的值用http_build_query()函數轉化成URL-ENCODE的形式才會以application/x-www-form-urlencoded的方式進行請求。
1 <?php 2 3 $curl = curl_init(); 4 5 curl_setopt_array($curl, array( 6 CURLOPT_URL => "http://www.baidu.com", 7 CURLOPT_RETURNTRANSFER => true, 8 CURLOPT_ENCODING => "", 9 CURLOPT_MAXREDIRS => 10, 10 CURLOPT_TIMEOUT => 30, 11 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 12 CURLOPT_CUSTOMREQUEST => "POST", 13 CURLOPT_POSTFIELDS => "s1=red&s2=blue&undefined=", 14 CURLOPT_HTTPHEADER => array( 15 "Content-Type: application/x-www-form-urlencoded", 16 "cache-control: no-cache" 17 ), 18 )); 19 20 $response = curl_exec($curl); 21 $err = curl_error($curl); 22 23 curl_close($curl); 24 25 if ($err) { 26 echo "cURL Error #:" . $err; 27 } else { 28 echo $response; 29 }
另外再補充一下,form-data 是可以添加文件上傳的,因為是按照提交表單的方式來發送數據的,所以PHP的curl默認采用這種方式,而x-www-form-urlencoded 是不可以上傳文件的。
用postman以raw發送的例子:

1 <?php 2 3 $curl = curl_init(); 4 5 curl_setopt_array($curl, array( 6 CURLOPT_URL => "http://www.baidu.com", 7 CURLOPT_RETURNTRANSFER => true, 8 CURLOPT_ENCODING => "", 9 CURLOPT_MAXREDIRS => 10, 10 CURLOPT_TIMEOUT => 30, 11 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 12 CURLOPT_CUSTOMREQUEST => "POST", 13 CURLOPT_POSTFIELDS => "{\"s1\":\"red\",\"s2\":\"blue\"}", 14 CURLOPT_HTTPHEADER => array( 15 "Content-Type: application/json", 16 "cache-control: no-cache" 17 ), 18 )); 19 20 $response = curl_exec($curl); 21 $err = curl_error($curl); 22 23 curl_close($curl); 24 25 if ($err) { 26 echo "cURL Error #:" . $err; 27 } else { 28 echo $response; 29 }
所以,在使用PHP的curl進行遠程請求數據的時候,不要盲目的使用,應該要注意選擇何種請求的方式。
————————————————
版權聲明:本文為CSDN博主「神男魯班」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/leigin/article/details/89358126
