I've been looking for a PHP external class that can send and receive data from YouTube.
The idea is to have the comments and the rating (using stars or a dropdown list) on my own site. I have googleed for some scripts but there is so much to offer.
I like to know if there is someone who has experience and knows a script that is good and works efficient. I have basic knowledge of PHP so it would be nice if there is a class with a clear example of how to apply.
Youtube API
Re: Youtube API
here is a couple of functions I pulled from a script, original author unknown:
save above as yt_class.php
save below as example.php:
Code: Select all
<?
class youtube
{
public $url;
public $id;
public function url2id()
{
$aux = explode("?",$this->url);
$aux2 = explode("&",$aux[1]);
foreach($aux2 as $field => $value)
{
$aux3 = explode("=",$value);
if($aux3[0] == 'v') $video = $aux3[1];
}
return $this->id = $video;
}
public function info()
{
$feedURL = 'http://gdata.youtube.com/feeds/base/videos?q='.$this->id.'&client=ytapi-youtube-search&v=2';
$sxml = simplexml_load_file($feedURL);
foreach ($sxml->entry as $entry)
{
$details = $entry->content;
$info["title"] = $entry->title;
}
$details_notags = strip_tags($details);
$texto = explode("From",$details_notags);
$info["description"] = $texto[0];
$aux = explode("Views:",$texto[1]);
$aux2 = explode(" ",$aux[1]);
$info["views"] = $aux2[0];
$aux = explode("Time:",$texto[1]);
$aux2 = explode("More",$aux[1]);
$info["time"] = $aux2[0];
$imgs = strip_tags($details,'<img>');
$aux = explode("<img",$imgs);
array_shift($aux);
array_shift($aux);
$aux2 = explode("gif\">",$aux[4]);
array_pop($aux);
$aux3 = $aux2[0].'gif">';
$aux[] = $aux3;
$imagens = '';
foreach($aux as $field => $value)
{
$imagens .= '<img'.$value;
}
$info["stars"] = $imagens;
return $info;
}
public function player($width,$height)
{
$this->url2id();
print '<object width="'.$width.'" height="'.$height.'"><param name="movie" value="http://www.youtube.com/v/'.$this->id.'&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$this->id.'&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'.$width.'" height="'.$height.'"></embed></object>';
}
}
?>
save below as example.php:
Code: Select all
<?
include "yt_class.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<__html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Youtube Class</title>
</head>
</__body>
<?
$obj = new youtube;
$obj->url = "http://www.youtube.com/watch?v=8B7s01DpBTg";
$obj->player("480","385");
print '<br>';
print '<br>';
$info = $obj->info();
print $info["title"];
print '<br>';
print $info["description"];
print '<br>';
print $info["views"];
print '<br>';
print $info["time"];
print '<br>';
print $info["stars"];
print '<br>';
?>