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

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

Related

PHP Wont print from external API

I use to go to school for only a short time in programming. A while ago and I'm rusty. I've been trying to re-learn everything by myself and something is bothering me. I'm trying to print out a specific object from an external API but nothing I try seems to work out. I don't really know what to google to get the right answer I am looking for. Anyway here's my code.
<?php
$url = 'http://apis.is/flight?language=en&type=departures';
$json = file_get_contents($url);
$results = json_decode($json, TRUE);
for ($x = 0; $x < count($results); $x++) {
echo $results[$x]['results']['flightNumber']."<br/>";
}
?>
If you do a debug (by the way, learn what is it), you will see, that your $results has one key: result, over which you can iterate with a simple foreach:
foreach ($results['result'] as $item) {
echo $item['flightNumber'];
}
You are trying to access the data returned from the API in the wrong order, do this instead:
<?php
$url = 'http://apis.is/flight?language=en&type=departures';
$json = file_get_contents($url);
$results = json_decode($json, TRUE);
// To loop through an array, use foreach instead of for
// It is easier to use
foreach($results['results'] as $result){
echo $result['flightNumber'].'<br />';
}
?>
<?php
$url = 'http://apis.is/flight?language=en&type=departures';
$json = file_get_contents($url);
$results = json_decode($json, TRUE);
foreach ($results['results'] as $res) {
echo $res['flightNumber']."<br/>";
}
?>

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 parse Json result?

Please help me get JSON result - "col1":"64.7020" to $result.
{"query":{"count":1,"created":"2016-08-28T09:50:07Z","lang":"ru-RU","diagnostics":{"publiclyCallable":"true","url":{"execution-start-time":"1","execution-stop-time":"2","execution-time":"1","content":"http://finance.yahoo.com/d/quotes.csv?e=.csv&f=c4l1&s=USDRUB=X"},"user-time":"2","service-time":"1","build-version":"0.2.48"},"results":{"row":{"col0":"RUB","col1":"64.7020"}}}}
$tick=file_get_contents('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fe%3D.csv%26f%3Dc4l1%26s%3DUSDRUB%3DX%22%3B&format=json&diagnostics=true&callback=');
$url = $tick;
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$result ??
echo col1 ???????
You have called file_get_contents twice. $tick will have the json returned by yahoo
$tick = file_get_contents('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fe%3D.csv%26f%3Dc4l1%26s%3DUSDRUB%3DX%22%3B&format=json&diagnostics=true&callback=');
$json_a = json_decode($tick, TRUE);
echo $json_a["query"]["results"]["row"]["col1"];

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.

Pulling YouTube viewCount

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

Categories