PHP CURL POST 請求設置 Content-Type (指定Content-Type)


Content-Type(內容類型),一般是指網頁中存在的 Content-Type,用於定義網絡文件的類型和網頁的編碼,決定瀏覽器將以什么形式、什么編碼讀取這個文件,這就是經常看到一些 PHP 網頁點擊的結果卻是下載一個文件或一張圖片的原因。

Content-Type 標頭告訴客戶端實際返回的內容的內容類型。

語法格式

Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something
Content-Type: application/json
Content-Type: application/x-www-form-urlencoded

手動指定Content-Type:

 1 function curl_post_send($url, $params, $header)
 2 {
 3     $ch = curl_init();
 4     curl_setopt($ch, CURLOPT_POST, 1);
 5     curl_setopt($ch, CURLOPT_URL, $url);
 6     curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
 7     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 8     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 9     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
10     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
11     $return_content = curl_exec($ch);
12     curl_close($ch);
13     return $return_content;
14 }
15 
16 $head = array();
17 $head[] = 'Content-Type: application/json';
18 $url = "https:www.xxx.com";
19 $data = ['a' => 1];
20 $data = json_encode($data);
21 $res = json_decode(curl_post_send($url, $data, $head));
22 echo json_encode($res);

END...

參考:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Type

     https://www.runoob.com/http/http-content-type.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM