0

I'm trying to download json data using curl in PHP from companies house API.

Here is the example they provide that I'm trying to use: https://developer.companieshouse.gov.uk/api/docs/company/company_number/readCompanyProfile.html#here

The example:

curl -uYOUR_APIKEY_FOLLOWED_BY_A_COLON: https://api.companieshouse.gov.uk/company/{company_number} 

My code

<?php $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://api.companieshouse.gov.uk/company/01000000"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'curl -u8yUXrestOfmyApiKey:' )); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); $result = curl_exec($curl); if(false == $result){ echo curl_error($curl); } curl_close($curl); print($result); ?> 

Using header:

'-u my_api_key:8yUXGgcHznjaNWnKpuKIJ7yv7HDvU_slH273GuF1' 

I get 400 - bad request and with header in my code above I get nothing no errors blank page no data.

PS. There is a company with number 01000000 I've checked

3
  • 1
    -u is NOT a header. it's used to pass a user:pass pair to curl. you want CURLOPT_USERPWD instead. Commented Jun 10, 2016 at 14:56
  • @MarcB they say in the documentation that I should only pass username as API_key and leave the password blank. Commented Jun 10, 2016 at 15:02
  • irrelevant. curl -u is not a valid http header. you have use curlopt_userpwd. whether you provide an actual pw or not is up to you, but what you're doing is invalid. Commented Jun 10, 2016 at 15:03

1 Answer 1

1

You shuld add CURLOPT_USERPWD

curl_setopt($curl, CURLOPT_USERPWD, "YOUR API KEY"); #Add your api key

Sign up to request clarification or add additional context in comments.

4 Comments

the Companies House API takes the username as the API key and ignores the password, so can be left blank
Using curl -u8yUXGgcHznjaNWnKpuKIJ7yv7HDvU_slH273GuF1: https://api.companieshouse.gov.uk/company/01000000 in a command line it displays the json data..
@Alex..Hello sir..Can you please help me to solve this issue....stackoverflow.com/questions/37967017/…
@Alex , hi alex please check my probelm: stackoverflow.com/questions/44900950/… thankyou

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.