Giancarlo
Giancarlo

Reputation: 399

Consume API with php and cURL

Hi I'm trying to consume an API.

I need to add the API key in the header API endpoint = http://overheid.io/api/kvk

if you want to test things, you can create an account here: https://overheid.io/auth/register Documentation is in Dutch and can be found here: https://overheid.io/documentatie/kvk

This is what I came up with but do not get passed authentication.

<?php 
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER => true);
curl_setopt($curl, CURLOPT_HTTPHEADER, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;   
?>

Edit: I've replaced the initial code part, with the solutions, but still no success.

Can anyone point me in the right direction?

Thanks!

Upvotes: 3

Views: 3072

Answers (1)

Chris Brand
Chris Brand

Reputation: 1990

You can try to use the CURLOPT_USERPWD option to pass the Authentication header. You also seemed to have some syntax errors.

<?php 
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_USERPWD, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;   

But, this API seem to use a custom header...so you can try the following if it the above does not work:

curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: yourkey")); 

UPDATE:

I used the following with an account I created on their site:

<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: key"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;

This worked for me when I replaced the "key" part with the 64 character key they provide.

Upvotes: 4

Related Questions