我试图从 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: fr、Accept-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/