Extracting data from JSON issue - php

I am trying to get some information about YouTube channel's from the YouTube API.
This is an example the output (using Google's channel), http://gdata.youtube.com/feeds/api/users/Google?alt=json
I am getting the JSON using this:
$json = file_get_contents("http://gdata.youtube.com/feeds/api/users/Google?alt=json");
$data = json_decode($json, true);
I uploaded the output of var_dump($data); to pastebin: http://pastebin.com/CWA7YYGi
What I want to get is totalUploadViews from yt$statistics.
What I have tried so far is:
echo $data['yt$statistics']['totalUploadViews'];
But this gives me an error: Notice: Undefined index: yt$statistics
Not sure what I am doing wrong, would appreciate the help.

yt$statistics is itself a value of a parent array. Try
$data['entry']['yt$statistics']['totalUploadViews'];

The yt$statistics is a key in the array of $data['entry'].

do this:
$json = file_get_contents("http://gdata.youtube.com/feeds/api/users/Google?alt=json");
$data = json_decode($json, true);
echo $data["entry"]["yt\$statistics"]["totalUploadViews"];
the "\$" just escapes the harmful $ from parsing.

Related

JSON - PHP - Reading Key

I have the following code below, which I am trying to run to save the key in the json to a PHP varible. However, for some reason it's just not doing anything - although it seems my code is right, any ideas where I am going wrong?
<?php
$json = file_get_contents('url');
$data = json_decode($json,true);
$id = $data['s_id'];
echo($id);
?>
and the json from the external URL looks like this
[{"s_id":"20063"}]
You have an array wrapping the object
$id = $data[0]['s_id'];

Parsing Instagram json data to usable variable in php

I have a working api call to get basic user info and I am trying to return just the "followed_by" portion of the json data from "counts".
This link should return just the json page on your browser
https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0
And here is my code to try and parse the result, but all I am getting is a blank screen.
$jsondata = file_get_contents("https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0");
$json = json_decode($jsondata, true);
$json2 = json_decode($jsondata);
//method one to parse, not working
$parsedvalue = $json['counts'][1]['followed_by'];
echo $parsedvalue;
//method two to parse, not working
$followercount = $json2->counts->followed_by;
echo $followercount;
Here is the fix I got figured out for //method 2
$jsondata = file_get_contents("https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0");
$json2 = json_decode($jsondata);
$followercount = $json2->data->counts->followed_by;
This properly returns your follower count, but I still need help understanding how to fix method one

json_decode for twitch api

Well, I am trying to use json_decode to get a users twitch name via their steam ID, however I am getting an error, and I have read other users issues and I am no closer to fixing it.
Here is my code:
$getcontents = file_get_contents('http://api.twitch.tv/api/steam/76561198049928469');
var_dump(json_decode($getcontents));
$twitchname = $getcontents ['name'];
echo $twitchname;
Here is my error:
Warning: Illegal string offset 'name' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\Portfolio -- Website\forum\index.php on line 29
I have looked at documentation on the dev forums on twitch and I cannot find a way to fix this.
I have also looked at answers on these forums and I cannot find a way to fix this.
Forgot this; vardump output:
object(stdClass)#2 (2) { ["_id"]=> int(59956494) ["name"]=> string(10) "riggster98" }
So the way it works with json_decode is, if you intend to use it like a normal php variable, then you have to pass the true flag into the json_decode function. Then you can use $content['name'] and get the expected results.
But if you want to work with Objects, you can simply just json_decode the content, and then use $content->name to extract the content.
Like this
$getcontents = file_get_contents('http://api.twitch.tv/api/steam/76561198049928469');
$contents = json_decode($getcontents);
echo "<pre>";
print_r($contents->name);
echo "</pre>";
This fixes the issues of not saving the JSON decoded content and passing the parameter to return an array instead of an object:
$getcontents = file_get_contents('http://api.twitch.tv/api/steam/76561198049928469');
$getcontents = json_decode($getcontents, true);
$twitchname = $getcontents['name'];
echo $twitchname;

Extract Data from JSON URL

http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json
is the url, which contains json data. I want to use that data and send SMS to a particular phone no if value of latitude and longitude(Extracted from JSON).
Check constraints, we can use through php. But the main problem is How to extract data from JSON file?
I don't want to give you the solution, so the below should be enough to get you started.
Read in the JSON data using file_get_contents
Parse the JSON string into a PHP object using json_decode
Your code should look something like this:
$url = "http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json";
$contents = file_get_contents($url);
$jsonObj = json_decode($contents);
You mean something like this?
<?php
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->trends as $trend )
{
echo "{$trend->name}\n";
}

Retrieving info from JSON array with PHP

i have this public JSON https://raw.github.com/currencybot/open-exchange-rates/master/latest.json and i need to extract data from it, right now i tried something like this:
$input = file_get_contents("https://raw.github.com/currencybot/open-exchange-rates/master/latest.json");
$json = json_decode($input);
echo $json[rates]->EUR;
but i obtain a blank page, any suggestion? Thanks !! Ste
json_decode either returns an object, or an array (when you use the second param). You are trying to use both.
Try:
$json = json_decode($input);
echo $json->rates->EUR;
OR
$json = json_decode($input, true);
echo $json['rates']['EUR'];
As to the blank page, please add the following to the top of your script:
error_reporting(E_ALL);
init_set('display_errors', true);
A 500 Error indicates you are unable to resolve that url using file_get_contents.
Check here for more information.
Read the help for json_decode ... without the second parameter it returns and object not an array ...
Either :
$json = json_decode($input);
echo $json->rates->EUR;
or
$json = json_decode($input,true);
echo $json['rates']['EUR'];
Try:
$arr = json_decode($input, true);
var_dump($arr);
echo $arr['rates']['EUR'];
Further Reading:
http://de.php.net/manual/de/function.json-encode.php
For anexample with cURL and SSL read this:
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
The link looks like starting with https, while file_get_contents can't deal with SSL. My suggestion is using curl.

Categories