教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php curl调用接口显示HTTP Status 415如何解决

php curl调用接口显示HTTP Status 415如何解决

发布时间:2018-09-05   编辑:jiaochengji.com
教程集为您提供php curl调用接口显示HTTP Status 415如何解决等资源,欢迎您收藏本站,我们将为您提供最新的php curl调用接口显示HTTP Status 415如何解决资源
php函数cul调用远程地址时提示 HTTP Status 415 错误,折腾了好久才发现是没有指定Request Header 信息。下面来看具体解决过程。

使用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"));

您可能感兴趣的文章:
php curl调用接口显示HTTP Status 415如何解决
php cURL https链接报错:Unknown SSL protocol error in connection to
php使用curl函数提示Call to undefined function curl_init()
php curl post 时出现问题的解决方法
php中的curl使用心得详解
win7(X64)系统环境PHP Curl函数不能使用
php中curl、fsocket、file_get_content函数比较
php模拟登录qq邮箱(curl命令详解)
PHP Curl出现403错误怎么办?curl错误解决方法
php别人做好了接口怎么去调用

[关闭]
~ ~