Pulling YouTube viewCount - php

I'm trying to pull my viewcount from youtube through a php script and I'm having some issues.
My code is below. It doesn't seem to be working at all.
<?php
$video_ID = '<?php echo(types_render_field("youtube-id", array())); ?>';
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}? v=2&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>
Thanks so much!

I am not familiar with the api but i believe this is how it should be:
<?php
$video_ID = types_render_field("youtube-id", array());
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>
You had passed a string as the video id, see my changes

Related

How to use PHP to fetch and decode JSON data?

How can I get variables "price_usd" and "price_btc" from JSON url https://api.coinmarketcap.com/v1/ticker/ethereum/? I wrote a script but nothing happens.
<?php
$tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/');
$url = $tick;
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$usd = $data[0]["price_usd"];
echo $usd;
?>
Your code use a file_get_contents twice, look at would be:
<?php
$tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/');
$url = $tick;
echo $url;
//$json = file_get_contents($url);
$data = json_decode($tick, TRUE);
$usd = $data[0]["price_usd"];
echo $usd;
?>
Try this
<?php
$json = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/');
$data = json_decode($json);
var_dump($data[0]->price_usd);
?>

How to get number of video views with YouTube API V3?

i use this code to achieve this but this don't work :
<?php
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=hqepb5hzuB0&key={YOUR-API-KEY}");
$JSON_Data = json_decode($JSON);
$views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>
Thanks
Try something like this:
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=hqepb5hzuB0&key={YOUR-API-KEY}");
$json_data = json_decode($JSON, true);
echo $json_data['items'][0]['statistics']['viewCount'];

How can I parse this JSON with PHP?

How can I parse this JSON, which is supposed to display the items a user has in their Steam inventory.
I have tried this:
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
echo $data;
It returns the same as just visiting the link. I can't get anything like this to work either:
$id = $json->type;
echo $type;
This is how to get type
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
foreach ($json->rgDescriptions as $mydata)
{
echo $mydata->type;
}
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
echo $data;
you are echoing $data that is your input (so you see the same as opening the link directly). To see if the json_decode is working fine you should print $json.
So instead of
echo $data;
use
echo '<pre>'
print_r($json);
echo '</pre>';
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/');
$json = json_decode($data);
Now $json has 2 objects.
you can access like.
$json->rgInventory;
$json->success;
if you want to fetch all data from $json->rgInventory;
foreach($json->rgInventory as $e){
//var_dump($e);
echo $e->id;
echo $e->classid;
echo $e->instanceid;
}
etc.

Youtube feed array items in json showing as NULL

I am grabbing the comments from videos, but the "author name" and their id is giving NULL.
This is what I have.
$feedUrl='http://gdata.youtube.com/feeds/api/videos/'.$selected_video_id.'/comments?v=2&alt=json';
$data = json_decode(file_get_contents($feedUrl),true);
$info = $data["feed"];
$entry = $info["entry"];
$nEntry = count($entry);
for($i=0;$i<$nEntry;$i++){
$name = $entry[$i]['author']['name']['$t'];
$userId = $entry[$i]['author']['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];
}
Content and Published are collected fine, but the name and userID are not.
The feed does have the elements in it as have looked at it in the youtube data api demo beta. As well as it shows everything if you do a feed request in the browser.
http://gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments
So am I missing something?
It should be:
$name = $entry[$i]['author'][0]['name']['$t'];
$userId = $entry[$i]['author'][0]['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];
Or better yet:
$feedUrl=file_get_contents('http://gdata.youtube.com/feeds/api/videos/ASO_zypdnsQ/comments?v=2&alt=json');
$json = json_decode($feedUrl, true);
foreach($json['feed']['entry'] as $entry) {
echo $entry['author'][0]['name']['$t']."<br>";
echo $entry['author'][0]['yt$userId']['$t']."<br>";
echo $entry['content']['$t']."<br>";
echo $entry['published']['$t']."<br>";
}
You can use foreach like the above :)

json_decode file_get_contents help

<?php
$json = file_get_contents('http://tiny.cc/ttrhelp');
$obj = json_decode($json);
$example = $obj->rooms->displayName;
?>
Name: <?php echo $example; ?>
Trying to show the value for 'displayName' but its not showing
Untested code:
<?php
$json = file_get_contents('http://pub.tapulous.com/tapplications/coresocial/v1/chat/api/index.php?method=room_list');
$obj = json_decode($json);
foreach($obj->rooms as $room){
$example = $room->displayName;
echo $example;
}
?>
You probably want $obj->rooms[0]->displayName.
CodePad.
echo $obj->rooms[2]->displayName;
Try
echo $obj->rooms[0]->displayName;

Categories