[PHP] Lấy thông tin của Youtube Video
Trong bài viết trước tôi đã giới thiệu đến các bạn một số cách để lấy Youtube Video ID bằng PHP và hôm nay tôi sẽ sử dụng Video ID để lấy toàn bộ thông tin của Video đó.
Để hiểu hơn về những đoạn code dưới đây, các bạn nên tham khảo trước về Youtube API tại đây. Ngoài version 2 đang được sử dụng như phiên bản ổn định thì Youtube API cũng đã giới thiệu version 3 ở giai đoạn thử nghiệm. Các đoạn code tôi chia sẻ trong bài viết này sẽ hoạt động tốt với version 2 hơn.
Dữ liệu trả về của Youtube API thường là JSON nên đã hỗ trợ khá nhiều ngôn ngữ. Tôi tìm được một số tutorials đã sử dụng các ngôn ngữ khác nhau để lấy thông tin video. Các bạn có thể đọc thêm về những tutorial này.
- Retrieve Title, Description and Thumbnail of a YouTube Video Using JavaScript and AJAX
- Youtube Video Information from URL
- How to get youtube thumbnail and video information. (Using GAPI)
1, Kiểm tra Video còn tồn tại trên Youtube
Thường thì khi lấy thông tin Video, các bạn nên kiểm tra xem liệu video đó có còn tồn tại trên Youtube hay không. Và đoạn code đơn giản dưới đây sẽ giúp bạn nhanh chóng có được những thông tin cơ bản của video mà bạn đang thao tác.
<?php $youtubeID = "aI-p806k9AI"; $headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $youtubeID); print_r($headers); ?>
Và kết quả trả về nếu video đó vẫn còn tồn tại trên Youtube
Array ( [0] => HTTP/1.0 200 OK [1] => X-GData-User-Country: VN [2] => Content-Type: application/atom+xml; charset=UTF-8 [3] => Expires: Sun, 17 Mar 2013 07:41:30 GMT [4] => Date: Sun, 17 Mar 2013 07:41:30 GMT [5] => Cache-Control: private, max-age=300, no-transform [6] => Vary: * [7] => GData-Version: 1.0 [8] => Last-Modified: Sun, 17 Mar 2013 06:45:17 GMT [9] => X-Content-Type-Options: nosniff [10] => X-Frame-Options: SAMEORIGIN [11] => X-XSS-Protection: 1; mode=block [12] => Server: GSE )
Nếu video đó đã bị xóa hoặc Video ID không chính xác thì kết quả như sau
Array ( [0] => HTTP/1.0 400 Bad Request [1] => X-GData-User-Country: VN [2] => Content-Type: text/html; charset=UTF-8 [3] => Date: Sun, 17 Mar 2013 07:42:45 GMT [4] => Expires: Sun, 17 Mar 2013 07:42:45 GMT [5] => Cache-Control: private, max-age=0 [6] => X-Content-Type-Options: nosniff [7] => X-Frame-Options: SAMEORIGIN [8] => X-XSS-Protection: 1; mode=block [9] => Server: GSE )
Và đây là đoạn code để kiểm tra Video còn tồn tại trên Youtube như sau
<?php $youtubeID = "aI-p806k9AI"; $headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $youtubeID); if (strpos($headers[0], '200') !== false) { echo "The YouTube video already exists."; } else { echo "The YouTube video doesn't exist."; } ?>
2, Lấy thông tin của Youtube video
Sau khi đã chắc chắn biết video vẫn còn tồn tại thì chúng ta sẽ gởi yêu cầu đến Youtube để lấy thông tin dưới dạng JSON. Khi tham khảo nội dung trên trang Youtube API (version 2) thì tôi chú ý đến mục Searching for videos. Theo những chỉ dẫn trong mục đó, tôi đã gởi các tham số cần thiết để lấy được toàn bộ thông tin của video đó. Và đây đoạn code mà tôi sử dụng
<?php $video_ID = '66Wi3isw3NY'; $json = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json"); $json_data = json_decode($json); print_r($json_data); ?>
Trong đó, tham số v=2 là version mà Youtube API đang sử dụng ; alt=json là kiểu dữ liệu trả về.
3, Mở rộng
Tôi vô tình tìm được 1 đoạn code PHP cũng giúp cho các bạn lấy toàn bộ thông tin của Youtube Video, có điều là nó hơn dài dòng và sử dụng cURL để truy vấn. Các bạn có thể xem mã nguồn tại đây.
<?php // The Youtube's API url define('YT_API_URL', 'http://gdata.youtube.com/feeds/api/videos?q='); // Change below the video id. $video_id = '66Wi3isw3NY'; // Using cURL php extension to make the request to youtube API $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, YT_API_URL . $video_id); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Hold a rss feed xml returned by youtube API $feed = curl_exec($ch); curl_close($ch); // Using SimpleXML to parse youtube's feed $xml = simplexml_load_string($feed); $entry = $xml->entry[0]; // If no entry whas found, then youtube didn't find any video with specified id if(!$entry) exit('Error: no video with id "' . $video_id . '" whas found. Please specify the id of a existing video.'); $media = $entry->children('media', true); $group = $media->group; $title = $group->title; //$title: The video title $desc = $group->description; //$desc: The video description $vid_keywords = $group->keywords; //$vid_keywords: The video keywords $thumb = $group->thumbnail[0]; //There are 4 thumbnails, the first one (index 0) is the largest. //$thumb_url: the url of the thumbnail. $thumb_width: thumbnail width in pixels. //$thumb_height: thumbnail height in pixels. $thumb_time: thumbnail time in the video list($thumb_url, $thumb_width, $thumb_height, $thumb_time) = $thumb->attributes(); $content_attributes = $group->content->attributes(); // $vid_duration: the duration of the video in seconds. Ex.: 192. $vid_duration = $content_attributes['duration']; // $duration_formatted: the duration of the video formatted in "mm:ss". Ex.:01:54 $duration_formatted = str_pad(floor($vid_duration/60), 2, '0', STR_PAD_LEFT) . ':' . str_pad($vid_duration%60, 2, '0', STR_PAD_LEFT); // Show the variables for testing purposes: echo 'Title: ' . $title . ''; echo 'Desc: ' . $desc . ''; echo 'Video keywords: ' . $vid_keywords . ''; echo 'Thumbnail url: ' . $thumb_url . ''; echo 'Thumbnail width: ' . $thumb_width . ''; echo 'Thumbnail height: ' . $thumb_height . ''; echo 'Thumbnail time: ' . $thumb_time . ''; echo 'Video duration: ' . $vid_duration . ''; echo 'Video duration formatted: ' . $duration_formatted; ?>
Và kết quả nhận được
Title: 'Harry Potter and the Deathly Hallows: Part 2' Trailer Desc: http://www.hollywood.com Starring: Daniel Radcliffe, Rupert Grint, Emma Watson, Helena Bonham Carter, Jim Broadbent, Robbie Coltrane, Warwick Davis, Tom Felt… Video keywords: Thumbnail url: http://i.ytimg.com/vi/66Wi3isw3NY/0.jpg Thumbnail width: 360 Thumbnail height: 480 Thumbnail time: 00:00:57 Video duration: 114 Video duration formatted: 01:54
Hi vọng bài viết giúp ích cho các bạn. Thân ái!
Có cách nào kiểm tra rằng video nào đó cần phải xác nhận tuổi không bạn.
Với đối tượng JSON trả về thì bạn hãy đọc dữ liệu trong mảng accessControl thì thấy nếu video đó được cho phép (allowed) thì không có giới hạn về độ tuổi.
Ngoài ra bạn cũng có thể kiểm tra trong mảng media:rating để kiểm tra độ tuổi được xem video đó. Nếu không có mảng media:rating thì có nghĩa là mọi người đều có thể thoải mái xem video đó.
Tham khảo: http://stackoverflow.com/questions/12940901/youtube-api-2-0-checking-if-a-video-is-age-restricted
Chào bạn Tấn Việt! Mình mới tìm hiểu về php chưa được nhiều nên nhờ bạn giúp mình xíu. Với đoạn code trên mình muốn in ra thời gian (duration) của video và ảnh thumbnail của video thì phải làm sao??
Sorry!! ý mình là đoạn code chèn vào wordpress ấy.
Đường dẫn truyền vào sai rồi, phải là: http://gdata.youtube.com/feeds/api/videos?q={$video_ID}?v=2&alt=json” ^^
từ youtube được không.hướng dẫn chi tiết nha.minh do may cai vu nay lam
ban huong minh lay link rtsp từ youtube được không.hướng dẫn chi tiết nha.minh do may cai vu nay lam
Có cách nào biết được video đó có HD không bạn?
Cách này cũ rồi ko còn dùng đc