Retrieving the title, description, and views of YouTube videos

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

This gets the name. But how can I get an invoice and description? tried $youtube->description; and $youtube->views;

+6
source share
3 answers

I suggest you use JSON output instead of XML code.

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 need to download 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 find out which variables the JSON output has, add the prettyprint=true parameter to the URL and open it in your browser, it will decorate the JSON output to make it more clear:

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

Instead of viewing the url you can simply write

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

after

 $json = json_decode($json_output, true); 

and it will print formatted JSON output

+22
source

Youtube API V2.0 is deprecated. You need to switch to the V3 API. This requires an API key.

So they prefer to use, but in this way it does not work out to get a video description

 http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=ktt3C7nQbbA&format=json 
+2
source

My code is:

 $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:44 - After the critically acclaimed global hit of X-Men:

+1
source

Source: https://habr.com/ru/post/947894/


All Articles