2

I'm trying to get some JSON results from an api, but it always returned me this error:

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

I tried to put in my header: only the Accept-Language: fr, Accept-Language: fr, fr-ca, and all the header array you see below. There's my code

$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);

It's the first time that an api ask me for that and i doesn't know much about curl http headers. I tried this header too http://php.net/manual/en/function.curl-setopt.php#Hcom78046 but no results.

Thank you to help me and / or show me how to find the answer.

Mike Boutin
  • 5,297
  • 12
  • 38
  • 65

2 Answers2

4

From manual to curl_setopt http://www.php.net/curl_setopt

CURLOPT_HEADER TRUE to include the header in the output.

CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

For your code you should change CURLOPT_HEADER to CURLOPT_HTTPHEADER

newman
  • 2,689
  • 15
  • 23
1

You must determine whether it's an issue with the curl library (your client) or the receiving API.

There are two tools that I use often to help debug what is actually being sent from a given client:

  1. http://httpkit.com/wiretap
  2. http://fiddler2.com/

Wiretap is more likely to be useful to you in this case because fiddler is only really useful when the client is on your local machine (eg. your browser).

However, with fiddler, you can use its composer tool to craft a specific request so you can ensure that you're getting the response you expect from the API.

Alternately, you can ask curl to show you exactly what it's sending. However, I've had discrepancies with libraries before, so I try to use a tool outside of the library.

Community
  • 1
  • 1
Homer6
  • 15,034
  • 11
  • 61
  • 81