Api php curl integration

I'm 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 tried using the cURL code below without success. As for the GET method, I'm not sure how to pass the KEY API and LOGIN API 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; 
+4
source share
2 answers

HTTP headers should be specified using a colon as the delimiter between the key and the value, not the equal sign. Try the following:

 $header = array('Content-Type: application/xml', 'API-KEY: 1a2b3c4d5e6f7g8h9i', 'API-LOGIN: 1a2b3c4d5e6f7g8h9i'); 
+5
source

You can get some debugging information from curl and see what exactly does not 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 

Be sure to call it before closing and terminating the curl object

0
source

All Articles