Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve YouTube video details from video URL using PHP?

Using PHP, how can I get video information like title, description, thumbnail from a youtube video URL e.g.

http://www.youtube.com/watch?v=B4CRkpBGQzU 
like image 523
Ahmed Fouad Avatar asked Jun 05 '12 11:06

Ahmed Fouad


People also ask

How do I get the YouTube video ID from a URL?

The video ID will be located in the URL of the video page, right after the v= URL parameter. In this case, the URL of the video is: https://www.youtube.com/watch?v=aqz-KE-bpKQ. Therefore, the ID of the video is aqz-KE-bpKQ .


1 Answers

You can get data from youtube oembed interface in two formats: XML and JSON

Interface address: http://www.youtube.com/oembed?url=youtubeurl&format=json

Use this PHP function to get data

 function get_youtube($url){   $youtube = "http://www.youtube.com/oembed?url=". $url ."&format=json";   $curl = curl_init($youtube);  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  $return = curl_exec($curl);  curl_close($curl);  return json_decode($return, true);   }  $url = // youtube video url   // Display Data  print_r(get_youtube($url)); 

Don't forget to enable extension=php_curl.dll in your php.ini

like image 185
Navneet Singh Avatar answered Sep 20 '22 17:09

Navneet Singh