php - CURL php 不会接受 Accept-Language : fr needed to get the JSON

标签 php json api curl http-headers

我试图从 api 获取一些 JSON 结果,但它总是返回这个错误:

HTTP/1.1 400 Accept-Language HTTP header must be specified and be 'fr' or 'en' Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 10 Dec 2013 15:31:59 GMT Content-Length: 0

我尝试放入我的 header :仅 Accept-Language: frAccept-Language: fr、fr-ca 以及您看到的所有 header 数组以下。这是我的代码

$url = 'http://safire.afiexpertise.com/api/Courses?divisionId=2';

$header = array('Accept: application/json, text/javascript, */*; q=0.01','Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3','Accept-Encoding: gzip, deflate','Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7','Content-Type: application/json; charset=utf-8','X-Requested-With: XMLHttpRequest','Pragma: no-cache','Cache-Control: no-cache');

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, $header);
$data = curl_exec($curl);
curl_close($curl);

print_r($data);

这是 api 第一次向我询问这个​​问题,而且我对curl http headers 不太了解。我也尝试过这个标题http://php.net/manual/en/function.curl-setopt.php#Hcom78046但没有结果。

感谢您帮助我和/或告诉我如何找到答案。

最佳答案

从手动到curl_setopt http://www.php.net/curl_setopt

CURLOPT_HEADER TRUE 在输出中包含 header 。

CURLOPT_HTTPHEADER 要设置的 HTTP header 字段数组,格式为 array('Content-type: text/plain', 'Content-length: 100')

对于您的代码,您应该将 CURLOPT_HEADER 更改为 CURLOPT_HTTPHEADER

关于php - CURL php 不会接受 Accept-Language : fr needed to get the JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20498667/

相关文章:

ios - Swift 发布请求 - JSON 正文为空

api - yt :quota too many recent calls - Youtube API help

.htaccess - Lumen 从请求中获取 header key

php - 如何摆脱用 PHP 创建的表格顶部的额外换行符?

php - 使用 PHP 创建最近 30 天的数组

php - 将 mysql_fetch_array 结果转储到多维数组中

json - GWT RequestBuilder POST JSON 应用程序/json

php - 我定义了 15 个类,名称为 box1、box2、box3、...box15。如何在没有逗号分隔的情况下定义类?

java - Jackson 在将 JSON 反序列化为 POJO 时抛出 "msg":"Unrecognized field\"numeratorType\"

android - Google Vision API - 空对象引用上的人脸方法