Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to Insightly API using PHP and cURL

I've pasted my code below. I'm getting an empty string back, no curl error or anything.

$service_url = "https://api.insight.ly/v2.1/Opportunities";           
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "-my-base64-encoded-api-key");

$data = curl_exec($ch);
curl_close($ch);

since the documentation for the Insightly API said to leave password blank, i've also tried

 curl_setopt($ch, CURLOPT_USERPWD, "-my-base64-encoded-api-key:"); 

and

 curl_setopt($ch, CURLOPT_USERPWD, "-my-base64-encoded-api-key: ");

Any help would be appreciated. Thank you.

like image 370
Gratus D. Avatar asked Mar 22 '23 22:03

Gratus D.


1 Answers

Below is the working code - I had to send the authorization in the header, not as an option in CURL. I didn't know this was possible. Insightly support came through and provided me with additional instructions. I am posting the answer below in case someone else runs into this problem.

$service_url = 'https://api.insight.ly/v2.1/Opportunities';
$ch = curl_init($service_url);           
curl_setopt($ch, 
            CURLOPT_HTTPHEADER, 
            array('Content-Type: application/json', 
                  'Authorization: Basic my-base64-encoded-api-key'));
curl_setopt($ch , CURLOPT_HEADER, 0);           
curl_setopt($ch , CURLOPT_TIMEOUT, 30);           
curl_setopt($ch , CURLOPT_HTTPGET, 1);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, TRUE);            

$return = curl_exec($ch );    

curl_close($process);
like image 77
Gratus D. Avatar answered Apr 02 '23 09:04

Gratus D.