Don't like ads? PRO users don't see any ads ;-)
Guest

Simple PHP Youtube API - Swen Kooij

By: a guest on May 2nd, 2012  |  syntax: PHP  |  size: 9.71 KB  |  hits: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.                 // Simple Youtube API
  3.                 // Written by Swen Kooij
  4.                 // Version 1.0
  5.                
  6.                 // Function to check if a video exists
  7.                 function videoExists($id)
  8.                 {
  9.                         $Content = file_get_contents("http://www.youtube.com/watch?v=$id");
  10.                         /* if(strpos($Content, "The video you have requested is not available.") == false) return false; */
  11.                         return true;
  12.                 }
  13.                
  14.                 // Function to get between
  15.                 function getBetween($stringA, $stringB, $in)
  16.                 {
  17.                         $Array = explode($stringA, $in);
  18.                         $Arr = explode($stringB, $Array[1]);
  19.                         return $Arr[0];
  20.                 }
  21.                
  22.                 // Function to get multiple betweens
  23.                 function getMultiBetween($stringA, $stringB, $in)
  24.                 {
  25.                         $Strings = array();
  26.                         $Array = explode($stringA, $in);
  27.                         foreach($Array as $A)
  28.                         {
  29.                                 $AS = explode($stringB, $A);
  30.                                 $Strings[] = $AS[0];
  31.                         }
  32.                         return $Strings;
  33.                 }      
  34.                
  35.                 // Function to convert minutes to seconds
  36.                 function minToSec($min)
  37.                 {
  38.                         return $min * 60;
  39.                 }
  40.                
  41.                 // Check if func is set
  42.                 if(isset($_GET['func']) == false) die("error: func_not_set");
  43.                
  44.                 /* Function: getVideoPlayer */
  45.                 if($_GET['func'] == "getVideoPlayer")
  46.                 {
  47.                         // Check if id is set
  48.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  49.                        
  50.                         // Check if mode is set
  51.                         if(isset($_GET['mode']) == false) die("error: mode_not_set");
  52.                        
  53.                         // Check if prop is set
  54.                         if(isset($_GET['prop']) == false) die("error: prop_not_set");
  55.                        
  56.                         // Check if video id exists
  57.                         if(videoExists($_GET['id']) == false) die("error: invalid_id");
  58.                        
  59.                         // Build url based on given properties
  60.                         $Url = "http://www.youtube.com/embed/" . $_GET['id'];
  61.                         $Properties = explode(',', $_GET['prop']);
  62.                         foreach($Properties as $Prop)
  63.                         {
  64.                                 if($Prop == "https")
  65.                                 {
  66.                                         $Url = str_replace("http", "https", $Url);
  67.                                 }
  68.                                 else if($Prop == "nocookies")
  69.                                 {
  70.                                         $Url = str_replace("youtube", "youtube-nocookie", $Url);
  71.                                 }
  72.                         }
  73.                        
  74.                         // Check mode
  75.                         if($_GET['mode'] == "display")
  76.                         {
  77.                                 die(file_get_contents($Url));
  78.                         }
  79.                         else if($_GET['mode'] == "url")
  80.                         {
  81.                                 die($Url);
  82.                         }
  83.                         else die("error: invalid_mode");
  84.                 }
  85.                
  86.                 /* Function: getVideoDescription */
  87.                 else if($_GET['func'] == "getVideoDescription")
  88.                 {
  89.                         // Check if id is set
  90.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  91.                        
  92.                         // Check if prop is set
  93.                         if(isset($_GET['prop']) == false) die("error: prop_not_set");
  94.                        
  95.                         // Build url
  96.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  97.                        
  98.                         // Get file contents and description
  99.                         $Content = file_get_contents($Url);
  100.                         $Description = getBetween('<p id="eow-description" >', '</p>', $Content);
  101.                        
  102.                         // Check for properties
  103.                         $Properties = explode(',', $_GET['prop']);
  104.                         foreach($Properties as $Prop)
  105.                         {
  106.                                 if($Prop == "nohtml")
  107.                                 {
  108.                                         $Description = strip_tags($Description);
  109.                                 }
  110.                         }
  111.                        
  112.                         // Show description
  113.                         die($Description);
  114.                 }
  115.                
  116.                 /* Function: getVideoTitle */
  117.                 else if($_GET['func'] == "getVideoTitle")
  118.                 {
  119.                         // Check if id is set
  120.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  121.                        
  122.                         // Build url
  123.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  124.                        
  125.                         // Get file contents and title
  126.                         $Content = file_get_contents($Url);
  127.                         $Title = getBetween(' <meta property="og:title" content="', '">', $Content);
  128.                        
  129.                         // Show title
  130.                         die($Title);
  131.                 }
  132.                
  133.                 /* Function: getVideoViews */
  134.                 else if($_GET['func'] == "getVideoViews")
  135.                 {
  136.                         // Check if id is set
  137.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  138.                        
  139.                         // Build url
  140.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  141.                        
  142.                         // Get file contents and views
  143.                         $Content = file_get_contents($Url);
  144.                         $Views = getBetween('<span class="watch-view-count"><strong>', '</strong>', $Content);
  145.                        
  146.                         // Show views
  147.                         die($Views);
  148.                 }
  149.                
  150.                 /* Function: getVideoLikes */
  151.                 else if($_GET['func'] == "getVideoLikes")
  152.                 {
  153.                         // Check if id is set
  154.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  155.                        
  156.                         // Build url
  157.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  158.                        
  159.                         // Get file contents and likes
  160.                         $Content = file_get_contents($Url);
  161.                         $Likes = trim(getBetween('pan class="likes">', '</span>', $Content));
  162.                        
  163.                         // Show likes
  164.                         die($Likes);
  165.                 }
  166.                
  167.                 /* Function: getVideoDislikes */
  168.                 else if($_GET['func'] == "getVideoDislikes")
  169.                 {
  170.                         // Check if id is set
  171.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  172.                        
  173.                         // Build url
  174.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  175.                        
  176.                         // Get file contents and dislikes
  177.                         $Content = file_get_contents($Url);
  178.                         $Dislikes = getBetween('<span class="dislikes">', '</span>', $Content);
  179.                         $Dislikes = trim(substr($Dislikes, 0, 1));
  180.                        
  181.                         // Show dislikes
  182.                         die($Dislikes);
  183.                 }
  184.                
  185.                 /* Function: getVideoImage */
  186.                 else if($_GET['func'] == "getVideoImage")
  187.                 {
  188.                         // Check if id is set
  189.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  190.                        
  191.                         // Build url
  192.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  193.                        
  194.                         // Get file contents and image url
  195.                         $Content = file_get_contents($Url);
  196.                         $Image = getBetween('<meta property="og:image" content="', '">', $Content);
  197.                        
  198.                         // Show image url
  199.                         die($Image);
  200.                 }
  201.                
  202.                 /* Function: getVideUploader */
  203.                 else if($_GET['func'] == "getVideoUploader")
  204.                 {
  205.                         // Check if id is set
  206.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  207.                        
  208.                         // Build url
  209.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  210.                        
  211.                         // Get file contents and uploader
  212.                         $Content = file_get_contents($Url);
  213.                         $Uploader = trim(getBetween('class="yt-user-name author" rel="author" dir="ltr">', '</a>', $Content));
  214.                        
  215.                         // Show uploader
  216.                         die($Uploader);
  217.                 }
  218.                
  219.                 /* Function: getVideKeywords */
  220.                 else if($_GET['func'] == "getVideoKeywords")
  221.                 {
  222.                         // Check if id is set
  223.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  224.                        
  225.                         // Build url
  226.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  227.                        
  228.                         // Get file contents and keywords
  229.                         $Content = file_get_contents($Url);
  230.                         $Keywords = trim(getBetween('"watch5", "keywords": "', '", "cr', $Content));
  231.                         $Keywords = explode(',', $Keywords);
  232.                        
  233.                         // Loop trough keywords
  234.                         foreach($Keywords as $Keyword)
  235.                         {
  236.                                 echo $Keyword . $_GET['deli'];
  237.                         }
  238.                         die();
  239.                 }
  240.                
  241.                 /* Function: getVideUploadDate */
  242.                 else if($_GET['func'] == "getVideoUploadDate")
  243.                 {
  244.                         // Check if id is set
  245.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  246.                        
  247.                         // Build url
  248.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  249.                        
  250.                         // Get file contents and upload date
  251.                         $Content = file_get_contents($Url);
  252.                         $UploadDate = trim(getBetween('<span id="eow-date" class="watch-video-date" >', '</span>', $Content));
  253.                        
  254.                         // Show upload date
  255.                         die($UploadDate);
  256.                 }
  257.                
  258.                 /* Function: getVideDuration */
  259.                 else if($_GET['func'] == "getVideoDuration")
  260.                 {
  261.                         // Check if id is set
  262.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  263.                        
  264.                         // Build url
  265.                         $Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  266.                        
  267.                         // Get file contents and duration
  268.                         $Content = file_get_contents($Url);
  269.                         $Duration = trim(getBetween('Duration: <a href="#" onclick="yt.www.watch.player.seekTo(6*60+39);return false;">', '</a><br', $Content));
  270.                        
  271.                         // Show duration
  272.                         die($Duration);
  273.                 }
  274.                
  275.                 /* Function: getVideComments */
  276.                 else if($_GET['func'] == "getVideoComments")
  277.                 {
  278.                         // Check if id is set
  279.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  280.                        
  281.                         // Build url and get contents
  282.                         $Content = file_get_contents("http://www.youtube.com/all_comments?v=" . $_GET['id']);
  283.                        
  284.                         // Comments
  285.                         $CommentsHtml = array();
  286.                         $CommentsHtml = getMultiBetween('<li class="comment yt-tile-default "', ' </li>', $Content);
  287.                         $Comments = array();
  288.                        
  289.                         // Loop trough comment htmls
  290.                         foreach($CommentsHtml as $Comment)
  291.                         {
  292.                                 $Text = getBetween('<p>', '</p>', $Comment);
  293.                                 $Author = getBetween('class="yt-user-name " dir="ltr">', '</a>', $Comment);
  294.                                 if(trim($Text) != "") $Comments[] = $Author . '~' . $Text;
  295.                         }
  296.                        
  297.                         // Loop trough parsed comments
  298.                         $c = 0;
  299.                         foreach($Comments as $Comment)
  300.                         {
  301.                                 if(isset($_GET['limit']) == true)
  302.                                 {
  303.                                         if($_GET['limit'] == $c)
  304.                                         {
  305.                                                 die();
  306.                                         }
  307.                                         else
  308.                                         {
  309.                                                 echo utf8_decode($Comment) . $_GET['deli'];
  310.                                         }
  311.                                 }
  312.                                 else
  313.                                 {
  314.                                         echo utf8_decode($Comment) . $_GET['deli'];
  315.                                 }
  316.                                 $c += 1;
  317.                         }
  318.                         die();
  319.                 }
  320.                
  321.                 /* Function: getVideComments */
  322.                 else if($_GET['func'] == "getVideoCommentsCount")
  323.                 {
  324.                         // Check if id is set
  325.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  326.                        
  327.                         // Build url and get contents
  328.                         $Content = file_get_contents("http://www.youtube.com/all_comments?v=" . $_GET['id']);
  329.                        
  330.                         // Comments
  331.                         $CommentsHtml = array();
  332.                         $CommentsHtml = getMultiBetween('<li class="comment yt-tile-default "', ' </li>', $Content);
  333.                         $Comments = array();
  334.                        
  335.                         // Loop trough comment htmls
  336.                         foreach($CommentsHtml as $Comment)
  337.                         {
  338.                                 $Text = getBetween('<p>', '</p>', $Comment);
  339.                                 $Author = getBetween('class="yt-user-name " dir="ltr">', '</a>', $Comment);
  340.                                 if(trim($Text) != "") $Comments[] = $Author . '~' . $Text;
  341.                         }
  342.                        
  343.                         // Loop trough parsed comments
  344.                         $c = 0;
  345.                         foreach($Comments as $Comment)
  346.                         {
  347.                                 $c = $c + 1;
  348.                         }      
  349.                        
  350.                         // Show ammount of comments
  351.                         die($c);
  352.                         die();
  353.                 }
  354.                
  355.                
  356.                 /* Function: videoExists */
  357.                 else if($_GET['func'] == "videoExists")
  358.                 {
  359.                         // Check if id is set
  360.                         if(isset($_GET['id']) == false) die("error: id_not_set");
  361.                        
  362.                         // show true or false
  363.                         if(videoExists($_GET['id']) == true) die("true");
  364.                         else die("false");
  365.                 }
  366.                
  367.                 /* Function does not exists */
  368.                 else die("error: incorrect_func");
  369. ?>