Untitled


SUBMITTED BY: Guest

DATE: July 4, 2014, 12:43 a.m.

FORMAT: PHP

SIZE: 4.4 kB

HITS: 148925

  1. <?php
  2. function getYoutubeId($ytURL)
  3. {
  4. $urlData = parse_url($ytURL);
  5. //echo '<br>'.$urlData["host"].'<br>';
  6. if($urlData["host"] == 'www.youtube.com') // Check for valid youtube url
  7. {
  8. $ytvIDlen = 11; // This is the length of YouTube's video IDs
  9. // The ID string starts after "v=", which is usually right after
  10. // "youtube.com/watch?" in the URL
  11. $idStarts = strpos($ytURL, "?v=");
  12. // In case the "v=" is NOT right after the "?" (not likely, but I like to keep my
  13. // bases covered), it will be after an "&":
  14. if($idStarts === FALSE)
  15. $idStarts = strpos($ytURL, "&v=");
  16. // If still FALSE, URL doesn't have a vid ID
  17. if($idStarts === FALSE)
  18. die("YouTube video ID not found. Please double-check your URL.");
  19. // Offset the start location to match the beginning of the ID string
  20. $idStarts +=3;
  21. // Get the ID string and return it
  22. $ytvID = substr($ytURL, $idStarts, $ytvIDlen);
  23. return $ytvID;
  24. }
  25. else
  26. {
  27. //echo 'This is not a valid youtube video url. Please, give a valid url...';
  28. return 0;
  29. }
  30. }
  31. // function to parse a video <entry>
  32. function parseVideoEntry($youtubeVideoID)
  33. {
  34. $obj= new stdClass;
  35. // set video data feed URL
  36. $feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $youtubeVideoID;
  37. // read feed into SimpleXML object
  38. $entry = simplexml_load_file($feedURL);
  39. // get nodes in media: namespace for media information
  40. $media = $entry->children('http://search.yahoo.com/mrss/');
  41. $obj->title = $media->group->title;
  42. $obj->description = $media->group->description;
  43. // get video player URL
  44. $attrs = $media->group->player->attributes();
  45. $obj->watchURL = $attrs['url'];
  46. // get video thumbnail
  47. $attrs = $media->group->thumbnail[0]->attributes();
  48. $obj->thumbnailURL = $attrs['url'];
  49. // get <yt:duration> node for video length
  50. $yt = $media->children('http://gdata.youtube.com/schemas/2007');
  51. $attrs = $yt->duration->attributes();
  52. $obj->length = $attrs['seconds'];
  53. // get <yt:stats> node for viewer statistics
  54. $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
  55. $attrs = $yt->statistics->attributes();
  56. $obj->viewCount = $attrs['viewCount'];
  57. // get <gd:rating> node for video ratings
  58. $gd = $entry->children('http://schemas.google.com/g/2005');
  59. if ($gd->rating) {
  60. $attrs = $gd->rating->attributes();
  61. $obj->rating = $attrs['average'];
  62. } else {
  63. $obj->rating = 0;
  64. }
  65. // get <gd:comments> node for video comments
  66. $gd = $entry->children('http://schemas.google.com/g/2005');
  67. if ($gd->comments->feedLink) {
  68. $attrs = $gd->comments->feedLink->attributes();
  69. $obj->commentsURL = $attrs['href'];
  70. $obj->commentsCount = $attrs['countHint'];
  71. }
  72. return $obj;
  73. }
  74. $youtubeVideoLink = $_REQUEST['videoID'];
  75. //$youtubeVideoLink = 'http://tutsbucket.com/2010/11/css3-drop-down-menu/';
  76. $videoId = getYoutubeId($youtubeVideoLink);
  77. if($videoId == '0')
  78. {
  79. echo 'This is not a valid youtube video url. Please, give a valid url...';
  80. }
  81. else
  82. {
  83. $videoInfo = parseVideoEntry($videoId);
  84. // display main video record
  85. //echo "</table>";
  86. //echo '<br><br>';
  87. // read 'video comments' feed into SimpleXML object
  88. // parse and display each comment
  89. //if ($videoInfo->commentsURL && $videoInfo->commentsCount > 0)
  90. //{
  91. //$commentsFeed = simplexml_load_file($videoInfo->commentsURL);
  92. //echo "<tr><td colspan=\"2\"><h3>" . $commentsFeed->title .
  93. //"</h3></td></tr>\n";
  94. //echo "<tr><td colspan=\"2\"><ol>\n";
  95. //foreach ($commentsFeed->entry as $comment)
  96. }
  97. echo ''.$videoInfo->title;
  98. ?>

comments powered by Disqus