0

hi am new to curl but need it for a particular project could you help me format this code to work i would like to get the results and print out the raw JSON on the page here is the code i am using

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "curl -u api key: https://api.companieshouse.gov.uk/search/companies");
$x = curl_exec($curl);
curl_close($curl);
print($x);

this is a link to the api page i am trying to use https://developer.companieshouse.gov.uk/api/docs/search/companies/companysearch.html

this is the example they give on the page

curl -uYOUR_APIKEY_FOLLOWED_BY_A_COLON: 
https://api.companieshouse.gov.uk/search/companies

these are the parameters for the call if possible i would like to set them as well

q (required)
items_per_page (optional)
start_index (optional)
user2692997
  • 2,001
  • 2
  • 14
  • 20
  • Are you doing php code based curl or command line curl? I ask because of this `"curl -u api key: https://api.companieshouse.gov.uk/search/companies" it's a command line code.If you are doing php code based curl then use this code https://stackoverflow.com/a/32596799/6160662 and upvote the author if it works :) – Vinay Mar 20 '18 at 16:18
  • @Viney hey i know its asking a lot but can you please show me an example code for php specific to my need thanks in advance – user2692997 Mar 21 '18 at 09:09
  • Ok...first tell me you want to do it in a php script or from command line? – Vinay Mar 21 '18 at 16:02
  • @Viney no if possible i would like to run it from a web browser – user2692997 Mar 22 '18 at 08:55

2 Answers2

1

I simplified your request.you can try this by using below code.

$params = array("q"=>"<Some Value>","items_per_page"=>"<Some Value>","start_index"=>"<Some Value>");

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "api key:");
curl_setopt($curl, CURLOPT_URL, "https://api.companieshouse.gov.uk/search/companies?".http_build_query($params));
$x = curl_exec($curl);
curl_close($curl);
print($x);
0

Thanks for this opportunity to learn more of curl :-)

I tested the code below, it works. Of course you must be registered on the site and you must have got your API key. You will then provide it as username/password, but without username (this is written here on the site).

$searchvalue= 'Test';

$curl = curl_init("https://api.companieshouse.gov.uk/search/companies?q=$searchvalue");

curl_setopt($curl, CURLOPT_USERPWD, "your_api_key_provided_by_the_site");

// curl_setopt($curl, CURLOPT_HEADER, true); // when debugging : to show returning header

$rest = @curl_exec($curl);
print_r($rest);

if(curl_errno($curl))
{
    echo 'Curl error : ' . curl_error($curl);
}

@curl_close($curl);
b2vincent
  • 610
  • 7
  • 14
  • 1
    Disabling SSL security is like doing love without a condom with a strange looking babe without teeth and fresh needle marks on her forearm. Please never advice such thing to anybody publicly. Instead learn how to configure `curl` properly. – Mike Doe Mar 27 '18 at 10:25
  • OK, fixed. However the advice is easy to find on SO, not necessarily downvoted. – b2vincent Mar 27 '18 at 10:39