Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting title, description and number of views of a youtube video

Tags:

php

$youtube = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2');
$title = $youtube->title;

This gets the title. But how could I get the viewcount and description? tried $youtube->description; and $youtube->views;

like image 244
Kaka Avatar asked Jun 22 '13 12:06

Kaka


People also ask

Can YouTube titles give you a lot of views?

If you have a great title, it will grab the attention of the audience and you will get a lot of views. On the other hand, if you have a poor title or just a mediocre one, you won't get any views at all because there are more interesting videos out there.

Does changing description of YouTube title affect views?

The answer is yes, by changing the title or description you will either increase or decrease the number of views your video receives. The title will have a greater impact on viewership but both impact your YouTube Search Engine Optimization (SEO) score. How relevant your video is in regards to the viewer's search term.


2 Answers

I suggest you to use the JSON output instead of the XML one.

You can get it by adding the alt=json parameter to your URL:

http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json

Then you have to load the json and parse it:

<?php
$json_output = file_get_contents("http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json");
$json = json_decode($json_output, true);

//This gives you the video description
$video_description = $json['entry']['media$group']['media$description']['$t'];

//This gives you the video views count
$view_count = $json['entry']['yt$statistics']['viewCount'];

//This gives you the video title
$video_title = $json['entry']['title']['$t'];
?>

Hope this helps.

UPDATE

To see what variables does the JSON output have, add the prettyprint=true parameter to the URL and open it in your browser, it will beautify the JSON output to make it more comprehensible:

http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json&prettyprint=true

Instead of browsing the URL you can just write

echo "<pre>";
print_r($json);
echo "</pre>";

after

$json = json_decode($json_output, true);

and it will print the formatted JSON output

like image 115
BackSlash Avatar answered Nov 15 '22 08:11

BackSlash


My code:

$vId = "Jer8XjMrUB4";
$gkey = "AIzaSyCO5lIc_Jlrey0aroqf1cHXVF1MUXLNuR0";

$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=".$vId."&key=".$gkey."");


$data = json_decode($dur, true);
foreach ($data['items'] as $rowdata) {
     $vTime = $rowdata['contentDetails']['duration'];
     $desc = $rowdata['snippet']['description'];
}


$interval = new DateInterval($vTime);
$vsec = $interval->h * 3600 + $interval->i * 60 + $interval->s;

if($vsec > 3600)
    $vsec = gmdate("H:i:s", $vsec);
else
    $vsec = gmdate("i:s", $vsec);

echo $vsec."--".$desc;

Result:

02:47--Following the critically acclaimed global smash hit X-Men:

like image 31
NgocTinh Avatar answered Nov 15 '22 09:11

NgocTinh