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该怎么设置? 求大神帮忙解决代码没问题, 检查下其它地方是不是重新设置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";
一次请求的结果:
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。