PHP,Curl设置Content-type不成功,求大神帮忙解答,谢谢!

liuuu 发布于 2017/01/23 16:06
阅读 4K+
收藏 0

PHP用curl模拟post请求,接口那边规定要用content-type为multipart/form-data,

我用curl_getinfo()查看curl的请求头信息,发现,content-type却是application/json

代码如下:

public function getHttpResponsePOSTFILE($url,$para) {

   $curl = curl_init($url);
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
   curl_setopt($curl, CURLOPT_POST, true );
   curl_setopt($curl, CURLOPT_POSTFIELDS, $para);
   curl_setopt($curl, CURLOPT_HEADER, false);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   $responseText = curl_exec($curl);
   curl_close($curl);

   return $responseText;
}

接口那边接收到的content-type也是application/json。

请问该php该怎么设置?

求大神帮忙解决

加载中
0
bigTreee
bigTreee

代码没问题, 检查下其它地方是不是重新设置header了

---- demo, 发送请求: -----

<?php
function getHttpResponsePOSTFILE($url,$para) {

   $curl = curl_init($url);
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
   curl_setopt($curl, CURLOPT_POST, true );
   curl_setopt($curl, CURLOPT_POSTFIELDS, $para);
   curl_setopt($curl, CURLOPT_HEADER, false);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   $responseText = curl_exec($curl);
   curl_close($curl);

   return $responseText;
}
$para = array(
    "greeting"=>'hello world'
);
$resp = getHttpResponsePOSTFILE('http://localhost/PHP_Script/get_request.php',$para);
print_r($resp);
?>

==== 被请求的 URI: ====

<?php
$arr_headers = getallheaders();
$request_str = "Header---->\n" . print_r($arr_headers,true);
$request_str .= "POST---->\n" . print_r($_POST,true);
$request_str .= "GET---->\n" . print_r($_GET,true);
$request_str .= "==================request parameters shows end=============";
file_put_contents('request.txt',$request_str,FILE_APPEND);
echo "request has been report, thank you.<br />\n";

##### 一次请求的结果: #####

请求信息

返回顶部
顶部