This is now solved! Thanks to @voracityemail for their response. I’ve added some tweaks and it is now fully working. If you have any similar issues please find the working code below.
------ WORKING CODE ------
// Replace with the company number you want to search
$compno = "04176976";
$URL = "https://api.company-information.service.gov.uk/company/" . $compno . "/persons-with-significant-control";
$apikey = "___________________"; // API Key goes here
$acceptTypes = [ "application/json" ]; // any types you support - not strictly necessary here
$ch = curl_init();
$headers = [ 'Accept:' . implode(', ', $acceptTypes) ];
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $URL,
// Our API key is the username - we need username:password
CURLOPT_USERPWD => $apikey . ":",
CURLOPT_HTTPHEADER => $headers,
// Make security explicit!
CURLOPT_SSL_VERIFYPEER => true, // default true per cURL 7.1
CURLOPT_SSL_VERIFYHOST => 2, // Should be the default e.g. secure.
]);
$result = curl_exec($ch);
if (!curl_errno($ch)) {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// If request was successful
if ($http_code == "200") {
$decodedResult = json_decode($result);
// For each item from the result (each item corresponds to an individual PSC)
foreach ($decodedResult->items as $PSC) {
// echo $PSC->whichever field you want to collect
echo $PSC->name;
}
// View the entire response in a nice format
// echo "<pre>" . print_r($decodedResult, true) . "</pre>";
}
else {
echo "Http code: " . $http_code;
}
}
else {
echo curl_error($ch);
}
curl_close($ch);
---- ORIGINAL POST -----
Hey, sorry i’m sure i’m doing something really obviously wrong but I can’t see what it is and i’ve looked at some similar articles and haven’t found any solutions so I thought I’d give posting here a go.
I’ve just started working with the API today and tried to follow the example at
I’m working in PHP however so I’ve written it as follows:
$URL = "https://api.company-information.service.gov.uk/company/01234567/persons-with-significant-control";
$ch = curl_init();
$header = array();
$header[] = "Content-Length: 0";
$header[] = "Content-type: application/json; charset=utf-8";
$header[] = "Authorization: Basic ---API KEY----";
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$request = curl_exec($ch);
curl_close($ch);
var_dump($request);
where the —API KEY— is obviously replaced with my API key. I’m getting the repsonse back as:
{
"error": "Invalid Authorization header",
"type": "ch:service"
}
I’ve also tried doing this directly through Postman Canary and I’m getting the same response.
If anyone has any advice or could point me in a certain direction to figure it out myself I’d be immensely grateful. Again I’m sure i’m just missing something obvious and that this is a trivial issue so apologies in advance.
For extra detail incase it is somehow relevant, I have no restricted IP’s or specified my own IP/added a host and Javascript access disabled for the Application.
Thanks in advance.