PHP curl returns NULL

Hi,

I’d really appreciate some help…many thanks in advance!

The API seems to be returning empty result as follows from my browser:

string(0) “” CURL ERROR Message:
NULL Returned result:

Here is my PHP code…$username is set to my API key and $password is set to = ‘’

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://api.companieshouse.gov.uk/company/06643052’);
$encodedAuth = base64_encode($username.":".$password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Authentication : Basic’, ‘Content-Type: application/json’));
curl_setopt($ch, CURLOPT_USERPWD, $encodedAuth);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “GET”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $chresults = curl_exec($ch);
    $error_msg = curl_error($ch);
    $obj = json_decode($chresults, true);
    
    echo "CURL ERROR Message: ".var_dump($error_msg);
    echo "<br>";
    echo "Returned result: ".var_dump($obj);

    curl_close($ch);
    die();

Looks like the http Basic authentication is not being sent correctly. This should work:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.companieshouse.gov.uk/company/06643052');
$userpwd = $username.":".$password; // new
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) ); // change
curl_setopt($ch, CURLOPT_HTTPAUTH) = CURLAUTH_BASIC; // new
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$chresults = curl_exec($ch);
$error_msg = curl_error($ch);
if ($chresults !== false) {
    $http_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); // new
    // you didn't specify a type to accept so check this is what you think
    if (curl_getinfo($ch, CURLINFO_CONTENT_TYPE)=='application/json')
        $arr = json_decode($chresults, true);  // this returns an array - no 2nd param -> object
}

echo "CURL ERROR Message: ".htmlspecialchars(var_dump($error_msg)); // encode for the browser!
echo "<br>Response code: ".htmlspecialchars(var_dump($http_code)); // new
echo "<br>Returned result: ".htmlspecialchars(var_dump($arr));

curl_close($ch);
die();
  1. You’re using both CURLOPT_HTTPHEADER to set the authentication method but the header would be “Authorization” not “Authentication”. You’d also need the base64 encoded username + “:” + password string there. See https://en.wikipedia.org/wiki/Basic_access_authentication
  2. You’re also using the Curl option which does the same thing (USERPWD) but this does the base_64 encoding for you. You should combine this option with the one for http basic: curl_setopt($ch, CURLOPT_HTTPAUTH) = CURLAUTH_BASIC;

You’ve not specified types you want to accept (e.g. using the header 'Accept: ’ + comma separated list of types) - so I’ve added a check in the code above that you’re getting the type you expect. It always should be for this endpoint, but if you get into requesting documents this will change.

I’ve also added the http code - you’ll want to check this to see what if any errors you’ve got.

If you use the documents API you may want to not automatically follow redirects (as you’ve done with CURLOPT_FOLLOWLOCATION). In these cases some people report that because curl is sending their basic Authorization with each request, when the request gets to the document data servers (Amazon, not CH) they get rejected as obviously CH authorization is not recognised by Amazon’s servers. Curl allows you to grab returned http headers (see CURLOPT_HEADERFUNCTION) which you can use to see where you’re being redirected to. See my post on the following thread: