PHP script for fetching youtube videos information using keywords and parameters



I am currently trying to find a script which can be used for fetching details from youtube using keywords and parameters. What i want in my script is that, the php script should search videos for a particular keyword (eg: people are awesome) with a duration parameter (time > 900 sec) then fetching details regarding those videos (thumbnail url, video url, duration, desc etc). After searching a while i came across this script in ibm



<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://ift.tt/kkyg93">
<html xmlns="http://ift.tt/lH0Osb" xml:lang="en" lang="en">
<head>
<title>Searching for videos by keyword</title>
<style>
img {
padding: 2px;
margin-bottom: 15px;
border: solid 1px silver;
}
td {
vertical-align: top;
}
td.line {
border-bottom: solid 1px black;
}
</style>
</head>
<body>
<?php
// if form not submitted
// display search box
if (!isset($_GET['submit'])) {
?>
<h1>Keyword search</h1>
<form method="get" action="<?php echo
htmlentities($_SERVER['PHP_SELF']); ?>">
Keywords: <br/>
<input type="text" name="vq" />
<p/>
Items sorted by: <br/>
<select name="s">
<option value="viewCount">User views</option>
<option value="rating">User rating</option>
<option value="published">Publication time</option>
</select>
<p/>
Items per page: <br/>
<select name="i">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<p/>
<input type="submit" name="submit" value="Search"/>
</form>
<?php
// if form submitted
} else {
// check for search keywords
// trim whitespace
// encode search string
if (!isset($_GET['vq']) || empty($_GET['vq'])) {
die ('ERROR: Please enter one or more search keywords');
} else {
$vq = $_GET['vq'];
$vq = ereg_replace('[[:space:]]+', ' ', trim($vq));
$vq = urlencode($vq);
}

// set max results per page
if (!isset($_GET['i']) || empty($_GET['i'])) {
$i = 25;
} else {
$i = htmlentities($_GET['i']);
}

// set sort critera
if (!isset($_GET['s']) || empty($_GET['s'])) {
$s = 'viewCount';
} else {
$s = htmlentities($_GET['s']);
}

// set start index
if (!isset($_GET['pageID']) || $_GET['pageID'] <= 0) {
$o = 1;
} else {
$pageID = htmlentities($_GET['pageID']);
$o = (($pageID-1) * $i)+1;
}

// generate feed URL
$feedURL = "http://ift.tt/19UjAAd
?vq={$vq}&orderby={$s}&max-results={$i}&start-index={$o}";

// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);

// get summary counts from opensearch: namespace
$counts = $sxml->children('http://ift.tt/13zUF5b');
$total = $counts->totalResults;
$startOffset = $counts->startIndex;
$endOffset = ($startOffset-1) + $counts->itemsPerPage;

// include Pager class
require_once 'Pager/Pager.php';
$params = array(
'mode' => 'Jumping',
'perPage' => $i,
'delta' => 5,
'totalItems' => $total,
);
$pager = & Pager::factory($params);
$links = $pager->getLinks();
?>

<h1>Search results</h1>
<?php echo $total; ?> items found.
Showing items <?php echo $startOffset; ?> to
<?php echo $endOffset; ?>:
<p/>

<?php
// print page links
echo $links['all'];
?>

<table>
<?php
// iterate over entries in resultset
// print each entry's details
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://ift.tt/W3lYmr');

// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];

// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];

// get <yt:duration> node for video length
$yt = $media->children('http://ift.tt/IJLTKH');
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];

// get <yt:stats> node for viewer statistics
$yt = $entry->children('http://ift.tt/IJLTKH');
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['viewCount'];

// get <gd:rating> node for video ratings
$gd = $entry->children('http://ift.tt/17PKlnd');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}

// get video ID
$arr = explode('/',$entry->id);
$id = $arr[count($arr)-1];

// print record
echo "<tr><td colspan=\"2\" class=\"line\"></td></tr>\n";
echo "<tr>\n";
echo "<td><a href=\"{$watch}\">
<img src=\"$thumbnail\"/></a></td>\n";
echo "<td><a href=\"{$watch}\">
{$media->group->title}</a><br/>\n";
echo sprintf("%0.2f", $length/60) . " min. | {$rating} user rating |
{$viewCount} views<br/>\n";
echo $media->group->description . "<br/>\n";
echo "<a href=\"details.php?id=$id\">More information</a>
</td>\n";
echo "</tr>\n";
}
}
?>
</table>
</body>
</html>


I copied above code and saved it as u.php in wamp server root,But after clicking search button i got this error


enter image description here



Deprecated: Function ereg_replace() is deprecated in C:\wamp\www\u.php on line 60

Warning: simplexml_load_file(http://ift.tt/1rgqhIM) [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\wamp\www\u.php on line 91

Warning: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: I/O warning : failed to load external entity &quot;http://ift.tt/1rgqeNe; in C:\wamp\www\u.php on line 91

Fatal error: Call to a member function children() on a non-object in C:\wamp\www\u.php on line 94


can you please tell me how i can rectify this errors in this script or can you suggest any php script like simplehtmldom.php which can be used to fetch videos from youtube using keywords and paramerters.


No comments:

Post a Comment