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

 

posted @ 2021-11-02 17:03  Evengod  阅读(4646)  评论(0编辑  收藏  举报