加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php curl调用接口显示HTTP Status 415怎么解决

发布时间:2023-09-27 11:10:06 所属栏目:PHP教程 来源:
导读:使用php curl的方式调用对方提供的接口,收到了如下错误提示

HTTP Status 415

The server refused this request because the request entity is in a format not supported by the requested resource for the
使用php curl的方式调用对方提供的接口,收到了如下错误提示

HTTP Status 415

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

Curl 的代码片段如下:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_NOBODY, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120); 
curl_setopt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData)); 
 
$data = curl_exec($ch); 
curl_close($ch); 
多次检查curl设置已经接口的说明没有发现问题。对方的服务器使用的是Tomcat 7, 一度怀疑是对方web配置有误,后来仔细研究文档,其中提到Response是jason格式文档,而上述curl中没有指定Request Header 信息, 所以尝试加入一个header, 结果问题解决。 代码如下:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));

 

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章