1

I have a CURL response that looks like this:

{"page_number":2,"items":[],"items_per_page":1,"kind":"search#companies","total_results":0,"start_index":1} 

All i need from that is the value of "total_results":0

so I am trying to get "total_results" like this:

$url = "https://api.companieshouse.gov.uk/search/companies?q=POOdfgdfgyygfhfgfgfghgfP&items_per_page=1&start_index=0";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "my_password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

$output = curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$info = curl_getinfo($ch);
curl_close($ch);

echo $output->total_results[0];

However, when I echo the $output->total_results[0];, I get nothing being echoed on my page. So i don't really know what I am doing wrong!

Could someone please advise on this issue?

any help would be appreciated.

4
  • 1
    php.net/manual/en/function.json-decode.php Commented Dec 22, 2015 at 18:57
  • @AbraCadaver, the data is already decoded on my PHP page. plus, the link you've posted is dead. Commented Dec 22, 2015 at 18:58
  • 1. you don't show it and nether did my crystal ball 2. no it's working Commented Dec 22, 2015 at 19:01
  • @AbraCadaver, oh, your crystal ball must be broken mate.. lol.. yeah ok pal. cheers. Commented Dec 22, 2015 at 19:34

1 Answer 1

7

What you got as a response is a JSON string. You can parse it to a stdClass object using json_decode:

$object = json_decode( $output );

Then you can access the field you want:

$total_results = $object->total_results;

Alternatively, you can parse the JSON string to an array:

$array = json_decode( $output, true );

and get the var:

$total_results = $array['total_results'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.