<?php
$videos = $_POST['videos'];
$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0';
$url = 'http://www.youtube.com/watch?v=';
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
List of videos (1 per line):<br />
<textarea name="videos" cols="60" rows="10"></textarea><br />
<input type="submit" value="Get Views"><br />
</form>
<br /><br />
<?
if (!isset($videos))
{
echo "Fill in the above form with a list of videos. <br />\n";
exit;
}
echo "Results: <br />\n";
foreach(explode("\n", $videos) as $video)
{
parse_str(parse_url($video, PHP_URL_QUERY), $url_vars);
echo "Video: " . $url . $url_vars['v'] . "<br />\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent );
curl_setopt($ch, CURLOPT_URL,$url . $url_vars['v']);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
if(curl_errno($ch))
{
echo "Error: " . curl_error($ch) . "<br />\n";
}
curl_close($ch);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$node = $xpath->query('//span[@class="watch-view-count"]/text()')->item(0);
echo "Views: " . $node->textContent . "<br />\n";
echo "-------------------------------------------------------------<br />\n";
}