First i created application.I got api key.
Then i want to integrate company search api in my website
I create a code using curl.But its response blank.Please help…Can’t understand where i have to use api key for get result.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, ‘https://api.companieshouse.gov.uk/search/companies?q=cognizant+technology+solution ’);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, ‘GET’);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
if($errno = curl_errno($curl)) {
$error_message = curl_strerror($errno);
echo “cURL error ({$errno}):\n {$error_message}”;
}
curl_close($curl);
echo $response;
The Companies House API uses HTTP Basic Authentication to transmit an API key between the client application and the server. Basic authentication is usually made up of a username and password pair; the Companies House API takes the username as the API key and ignores the password, so can be left blank.
Try adding this:
curl_setopt($curl, CURLOPT_USERPWD,“your_api_key ”);
1 Like
mfairhurst:
CURLOPT_USERPWD
many many thanks for your help.
Its work…
nhsyed2
December 23, 2017, 2:03am
4
Hi mfairhurst
I’m fairly new to development and just learning API’s. I find the above question and your answer helpful. What I would like to do is to use the company availabilty checker site “https://beta.companieshouse.gov.uk/company-name-availability ” to find if the company is available to register a new businees.
I have registered the application and got API key to use. How can I achieve my goal.
I used above example to achieve this but using different URL: https://api.companieshouse.gov.uk/search/companies ?
I’ve passed q=xyz-xyx-xyz as some random company name as below to get the result.
CODE:
<?php
$responseArray = "";
$companyNumbers = "";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.companieshouse.gov.uk/search/companies?q=O2');
curl_setopt($curl, CURLOPT_USERPWD,"my_Api_Key");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
if($errno = curl_errno($curl)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($curl);
//echo $response;
$responseArray = json_decode($response, true);
$companyNumbers = $responseArray['total_results'];
if ($companyNumbers >0){
echo 'Your company name is NOT available
';
}else {
echo 'Your company name is available
';
}
//print_r ($responseArray);
//echo "
";
//echo $CompanyNames;
?>
Browser output:
Your company name is NOT available
I’m I save using the above example for a commercial website. Thank you