4

I am trying to integrate an API, and in the API integration instructions it shows the following:

GET /offers.json or /offers.xml
Headers: API-KEY={your_key}, API-LOGIN={your_login}

CURL Example:

curl https://api.thewebsite.com/v1/offers.json -H 'API-KEY:
1a2b3c4d5e6f7g8h9i' -H 'API-LOGIN: 1a2b3c4d5e6f7g8h9i'

I've tried using the cURL code below without success. As for the GET method, I'm not sure how to pass the API KEY & API LOGIN as headers.

$header = array('Content-Type: application/xml', 'API-KEY=1a2b3c4d5e6f7g8h9i', 'API-LOGIN=1a2b3c4d5e6f7g8h9i');
$url = "https://api.thewebsite.com/v1/offers.xml";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$xml = curl_exec($curl);
curl_close($curl);

print $xml;
2
  • 1
    What happens when you execute that curl script? Commented Oct 25, 2011 at 18:15
  • I was getting a Invalid API key error, which would be due to the use of = rather than : as Kaivosukeltaja pointed out. Commented Oct 25, 2011 at 19:04

3 Answers 3

5

HTTP headers need to be specified using a colon as the separator between the key and the value, not an equal sign. Try this:

$header = array('Content-Type: application/xml', 'API-KEY: 1a2b3c4d5e6f7g8h9i', 'API-LOGIN: 1a2b3c4d5e6f7g8h9i');
Sign up to request clarification or add additional context in comments.

2 Comments

(doh) I had used the = trying something else and forgot to change it. Thanks!
I've been trying to only print the details of the first offer returned. However the entire XML is displayed. Even if I remove all print calls. ## begin snip ## <?xml version="1.0" encoding="UTF-8"?> <offers type="array"> <offer> <end-date type="datetime">2011-09-22T23:59:59Z</end-date> <details>First offer details here</details> <faq nil="true"></faq> <value type="float">495.0</value> ## end snip ## print $xml->offer[0]->details
0

You can get some debug information from curl, and see exactly what doesn't work for you:

print "<pre>\n";
print_r(curl_getinfo($curl));  // get error info
echo "\n\ncURL error number:" .curl_errno($curl); // print error info
echo "\n\ncURL error:" . curl_error($ch); 
print "</pre>\n";
curl_close($curl); // close curl session

Make sure to call it before you close and terminate the curl object

1 Comment

Thanks. I'll keep it handy for the future.
0

Try the following -

print "<pre>\n";
print_r(curl_getinfo($curl));  // get error info
echo "\n\ncURL error number:" .curl_errno($curl); // print error info
echo "\n\ncURL error:" . curl_error($ch); 
print "</pre>\n";
curl_close($curl); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.