PHP: How to Parse JSON From API call? - php

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.

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'];

Related

I have created an call back API to receive the JSON data but its not working

I have created a call back API to receive the JSON data after hitting my server. But somehow its not working as expected
This the response which i have to receive in my API
{"ERROR":0,"STATUS":1,"ORDERID":753093,"OPTRANSID":"2107938600"}
I have written the php file for this rcstatus.php
$url = 'http://softwarecompany.club/pks/recharge/b2b/rcstatus.php?status='.$_GET['status'].'&Orderid='.$_GET['Orderid'].'&Optransid='.$_GET['Optransid'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
$data = curl_exec($ch);
echo $data;
$json = json_decode($url,true);
$json['STATUS'];
Please help me to achieve this. I have never worked with JSON data before. So please help me to achieve it. Thanks in advance!!!
You need to replace:
$json = json_decode($url,true);
with
$json = json_decode($data,true);
Replace $url to $data in last when you get the response like this.
$url = 'http://softwarecompany.club/pks/recharge/b2b/rcstatus.php?status='.$_GET['status'].'&Orderid='.$_GET['Orderid'].'&Optransid='.$_GET['Optransid'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
$data = curl_exec($ch); // This is Your Response
$json = json_decode($data,true);
$json['STATUS'];

How to get JSON parameter from URL

Hello I'm coming from Get JSON object from URL
I tried to get marketCap->usd.
Can anyone tell me what I'm doing wrong please?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://coinmarketcap.northpole.ro/api/v5/ZCL.json');
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
echo $result->marketCap->usd;
Change echo $result->marketCap->usd; for echo $obj->marketCap->usd;
You have saved the data decoded in $obj.
Last line of your code should be
echo $obj->marketCap->usd;

Parsing object in PHP not working

I'm using this to get my response (only relevant code below):
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
$my_resp = $result['total_count'];
var_dump($my_resp);
echo $result gives me an object that appears to be valid JSON. Like this:
{
"total_count": 1
}
However, $my_resp comes back as NULL. What am I doing wrong here?
You need to turn it into an object from a string
$result = json_decode( curl_exec($ch));
or an associative array
$result = json_decode( curl_exec($ch), true );
If it is JSON, you should json_decode() it before trying to read fields.

Retrieve JSON using PHP from a URL

I am trying to hit an API and get some JSON back. I can’t seem to figure out what I am doing wrong here. I am getting null as the output.
Here is my PHP code:
<?php
$query_string_full = 'https://api.cityofnewyork.us/calendar/v1/search.htm?app_id=39563317lalaland&app_key=somethingsomething&categories=City%20Government%20Office';
$json = file_get_contents($query_string_full);
$obj = json_decode($json);
echo '<pre>'. json_encode($obj, JSON_PRETTY_PRINT) .'</pre>';
?>
The function file_get_contents doesn’t work with https. You should use the cURL functions instead.
<?php
$query_string_full = 'https://api.cityofnewyork.us/calendar/v1/search.htm?app_id=39563317&app_key=8396021a9bde2aad2eaf8ca9dbeca353&categories=City%20Government%20Office';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query_string_full);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
$obj = json_decode($json);
echo '<pre>'. json_encode($obj, JSON_PRETTY_PRINT) .'</pre>';
?>
I made this little function to return the json from a few different apis. You need to be using curl.
function exposeJSON ($apiUrl) {
$json_url = $apiUrl;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $json_url);
// Built in authentication if needed.
//curl_setopt($ch, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
// grab URL and pass it to the browser
$response = curl_exec($ch);
$return = json_decode($response[0], true);
// close cURL resource, and free up system resources
curl_close($ch);
return $return;
}
So you could do something like
$apiData = exposeJSON('urltoapi');
echo $apiData; // Should be the json.

php youtube json decode

I tried to use JSON decode to get the youtube API feed. However, when I paste the JSON result in http://www.jsonlint.com/ I noticed something like
"media$group": {
"media$category": [
Unfortunately some symbols are rejected by php. Here is my code, I tried to remove this $ symbol, but maybe not success. How do I solve this?
$url = 'http://gdata.youtube.com/feeds/api/videosq=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
...
}
Your problem is the usage of PHP identifiers to access the contents. The simplest solution here would be to get an array instead of an object:
$data = json_decode ( $json , $assoc = true );
This allows access to fields with:
echo $result['media$group']['media$description'];
If you want to keep the object syntax, that's possible with this kludge:
echo $result->{'media$group'}->{'media$category'};
(But arrays are safer here. You don't get a fatal error should the format change and properties be absent.)
This work:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos?q=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
var_dump($result);
}
?>

Categories