Youtube.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. class Videolist_Platform_Youtube implements Videolist_PlatformInterface
  3. {
  4. public function getType()
  5. {
  6. return "youtube";
  7. }
  8. public function parseUrl($url)
  9. {
  10. $parsed = parse_url($url);
  11. $id = '';
  12. if (! empty($parsed['host'])) {
  13. if ($parsed['host'] === 'youtu.be') {
  14. // short URLs
  15. $id = substr($parsed['path'], 1);
  16. } elseif ($parsed['host'] === 'www.youtube.com'
  17. && $parsed['path'] === '/watch'
  18. && ! empty($parsed['query'])) {
  19. // long URLs
  20. parse_str($parsed['query'], $query);
  21. if (! empty($query['v'])) {
  22. $id = $query['v'];
  23. }
  24. }
  25. }
  26. if ($id) {
  27. return array(
  28. 'video_id' => $id,
  29. );
  30. }
  31. return false;
  32. }
  33. public function getData($parsed)
  34. {
  35. $video_id = $parsed['video_id'];
  36. // adding v3 APi google (https://www.googleapis.com/youtube/v3/videos?id=<video_id>&key=<YOUR_API_KEY>&part=snippet
  37. // $buffer = file_get_contents('https://gdata.youtube.com/feeds/api/videos/'.$video_id);
  38. $buffer = file_get_contents('https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v='.$video_id.'&format=xml');
  39. $xml = new SimpleXMLElement($buffer);
  40. return array(
  41. 'title' => $xml->title,
  42. // 'description' => strip_tags($xml->content),
  43. 'thumbnail' => "https://img.youtube.com/vi/$video_id/default.jpg",
  44. );
  45. }
  46. }