<?php
// Simple Youtube API
// Written by Swen Kooij
// Version 1.0
// Function to check if a video exists
function videoExists($id)
{
$Content = file_get_contents("http://www.youtube.com/watch?v=$id");
/* if(strpos($Content, "The video you have requested is not available.") == false) return false; */
return true;
}
// Function to get between
function getBetween($stringA, $stringB, $in)
{
$Array = explode($stringA, $in);
$Arr = explode($stringB, $Array[1]);
return $Arr[0];
}
// Function to get multiple betweens
function getMultiBetween($stringA, $stringB, $in)
{
$Strings = array();
$Array = explode($stringA, $in);
foreach($Array as $A)
{
$AS = explode($stringB, $A);
$Strings[] = $AS[0];
}
return $Strings;
}
// Function to convert minutes to seconds
function minToSec($min)
{
return $min * 60;
}
// Check if func is set
if(isset($_GET['func']) == false) die("error: func_not_set");
/* Function: getVideoPlayer */
if($_GET['func'] == "getVideoPlayer")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Check if mode is set
if(isset($_GET['mode']) == false) die("error: mode_not_set");
// Check if prop is set
if(isset($_GET['prop']) == false) die("error: prop_not_set");
// Check if video id exists
if(videoExists($_GET['id']) == false) die("error: invalid_id");
// Build url based on given properties
$Url = "http://www.youtube.com/embed/" . $_GET['id'];
$Properties = explode(',', $_GET['prop']);
foreach($Properties as $Prop)
{
if($Prop == "https")
{
$Url = str_replace("http", "https", $Url);
}
else if($Prop == "nocookies")
{
$Url = str_replace("youtube", "youtube-nocookie", $Url);
}
}
// Check mode
if($_GET['mode'] == "display")
{
die(file_get_contents($Url));
}
else if($_GET['mode'] == "url")
{
die($Url);
}
else die("error: invalid_mode");
}
/* Function: getVideoDescription */
else if($_GET['func'] == "getVideoDescription")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Check if prop is set
if(isset($_GET['prop']) == false) die("error: prop_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and description
$Content = file_get_contents($Url);
$Description = getBetween('<p id="eow-description" >', '</p>', $Content);
// Check for properties
$Properties = explode(',', $_GET['prop']);
foreach($Properties as $Prop)
{
if($Prop == "nohtml")
{
$Description = strip_tags($Description);
}
}
// Show description
die($Description);
}
/* Function: getVideoTitle */
else if($_GET['func'] == "getVideoTitle")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and title
$Content = file_get_contents($Url);
$Title = getBetween(' <meta property="og:title" content="', '">', $Content);
// Show title
die($Title);
}
/* Function: getVideoViews */
else if($_GET['func'] == "getVideoViews")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and views
$Content = file_get_contents($Url);
$Views = getBetween('<span class="watch-view-count"><strong>', '</strong>', $Content);
// Show views
die($Views);
}
/* Function: getVideoLikes */
else if($_GET['func'] == "getVideoLikes")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and likes
$Content = file_get_contents($Url);
$Likes = trim(getBetween('pan class="likes">', '</span>', $Content));
// Show likes
die($Likes);
}
/* Function: getVideoDislikes */
else if($_GET['func'] == "getVideoDislikes")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and dislikes
$Content = file_get_contents($Url);
$Dislikes = getBetween('<span class="dislikes">', '</span>', $Content);
$Dislikes = trim(substr($Dislikes, 0, 1));
// Show dislikes
die($Dislikes);
}
/* Function: getVideoImage */
else if($_GET['func'] == "getVideoImage")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and image url
$Content = file_get_contents($Url);
$Image = getBetween('<meta property="og:image" content="', '">', $Content);
// Show image url
die($Image);
}
/* Function: getVideUploader */
else if($_GET['func'] == "getVideoUploader")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and uploader
$Content = file_get_contents($Url);
$Uploader = trim(getBetween('class="yt-user-name author" rel="author" dir="ltr">', '</a>', $Content));
// Show uploader
die($Uploader);
}
/* Function: getVideKeywords */
else if($_GET['func'] == "getVideoKeywords")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and keywords
$Content = file_get_contents($Url);
$Keywords = trim(getBetween('"watch5", "keywords": "', '", "cr', $Content));
$Keywords = explode(',', $Keywords);
// Loop trough keywords
foreach($Keywords as $Keyword)
{
echo $Keyword . $_GET['deli'];
}
die();
}
/* Function: getVideUploadDate */
else if($_GET['func'] == "getVideoUploadDate")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and upload date
$Content = file_get_contents($Url);
$UploadDate = trim(getBetween('<span id="eow-date" class="watch-video-date" >', '</span>', $Content));
// Show upload date
die($UploadDate);
}
/* Function: getVideDuration */
else if($_GET['func'] == "getVideoDuration")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url
$Url = "http://www.youtube.com/watch?v=" . $_GET['id'];
// Get file contents and duration
$Content = file_get_contents($Url);
$Duration = trim(getBetween('Duration: <a href="#" onclick="yt.www.watch.player.seekTo(6*60+39);return false;">', '</a><br', $Content));
// Show duration
die($Duration);
}
/* Function: getVideComments */
else if($_GET['func'] == "getVideoComments")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url and get contents
$Content = file_get_contents("http://www.youtube.com/all_comments?v=" . $_GET['id']);
// Comments
$CommentsHtml = array();
$CommentsHtml = getMultiBetween('<li class="comment yt-tile-default "', ' </li>', $Content);
$Comments = array();
// Loop trough comment htmls
foreach($CommentsHtml as $Comment)
{
$Text = getBetween('<p>', '</p>', $Comment);
$Author = getBetween('class="yt-user-name " dir="ltr">', '</a>', $Comment);
if(trim($Text) != "") $Comments[] = $Author . '~' . $Text;
}
// Loop trough parsed comments
$c = 0;
foreach($Comments as $Comment)
{
if(isset($_GET['limit']) == true)
{
if($_GET['limit'] == $c)
{
die();
}
else
{
echo utf8_decode($Comment) . $_GET['deli'];
}
}
else
{
echo utf8_decode($Comment) . $_GET['deli'];
}
$c += 1;
}
die();
}
/* Function: getVideComments */
else if($_GET['func'] == "getVideoCommentsCount")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// Build url and get contents
$Content = file_get_contents("http://www.youtube.com/all_comments?v=" . $_GET['id']);
// Comments
$CommentsHtml = array();
$CommentsHtml = getMultiBetween('<li class="comment yt-tile-default "', ' </li>', $Content);
$Comments = array();
// Loop trough comment htmls
foreach($CommentsHtml as $Comment)
{
$Text = getBetween('<p>', '</p>', $Comment);
$Author = getBetween('class="yt-user-name " dir="ltr">', '</a>', $Comment);
if(trim($Text) != "") $Comments[] = $Author . '~' . $Text;
}
// Loop trough parsed comments
$c = 0;
foreach($Comments as $Comment)
{
$c = $c + 1;
}
// Show ammount of comments
die($c);
die();
}
/* Function: videoExists */
else if($_GET['func'] == "videoExists")
{
// Check if id is set
if(isset($_GET['id']) == false) die("error: id_not_set");
// show true or false
if(videoExists($_GET['id']) == true) die("true");
else die("false");
}
/* Function does not exists */
else die("error: incorrect_func");
?>