Does anyone know how to use youtube's api? I downloaded it from here But i cant figure out how to use a form to send a link to it and get back the name, keywords, html, description and thumbnail link.....


Does anyone know how to get this info from youtube with php and or youtube's api

Recommended Answers

All 2 Replies

Hi,

The linked is based on zend extension.. If you have not work with zend framework before that would be very tricky.

If you just need the same functionality as zend without breaking the peggy bank, thene consider the following codes. I posted these codes on my previous post in this forum..

<?php
## script was written by PoorBoy from veedeoo.com/forum
## no warranty of any kind, so please don't even bother asking
## Can help during my freetime, but still no guarantee.
#### First we need a function fot get youtube video id
function getPatternFromUrl($url)
{
$url = $url.'&';
$pattern = '/v=(.+?)&+/';
preg_match($pattern, $url, $matches);
//echo $matches[1]; die;
return ($matches[1]);
}
## function above we will use later

## define our query for youtube
$query = "ac+dc";
## define the order by published or viewCount
## viewCount is case sensitive
## you can also do the publisher or author only, read the youtube API about this
$order ="published";
## define start index. Youtube allows you query 50 videos per page, but no more that 1000 per $query
## lets define start index to be 1 . Depending on the result of the query this should return at least 1000/50 = number of pages 
$s_page = 1;
## let's define the number of results we want to show on the page
## Like I said above max is 1000 and it is break down into 50 videos per $s_page.
$nvideos_max = 50;
## define format type format 5 is for the videos where the publishers that allows their videos to be embedded.
$format = 5 ;
## private videos is not going to be included in the result. Unless you want to spend a buck or two I can write it for you.

$yt_videoq = "http://gdata.youtube.com/feeds/api/videos?q=$query&orderby=$order&start-index=$s_page&max-results=$nvideos_max&v=2&format=$format";

## now lets's read the videoq feeds.
$yt_xml = simplexml_load_file($yt_videoq);
## we can show in list table whatever you want.
echo '<ul style="list-style: none;padding: 0; margin:0px auto;width:750px;">';
## let's get the feed and loop through the result
foreach ($yt_xml->entry as $veedeoo) {
$yt_media = $veedeoo->children('http://search.yahoo.com/mrss/');
##we need video player url
$p_attrs = $yt_media->group->player->attributes();
$p_url = $_attrs['url']; 
## the above contains the video id
## we need to extract it
## below will retun video id based on the function we wrote above.
$v_id = getPatternFromUrl($p_url); 
$video_id = $v_id;
## leave the $v_id for now, but we will come back to it later
## lets pick up the thumbnails attribute
$t_attrs = $yt_media->group->thumbnail[2]->attributes();
## this is our thumbnail url.
$thumbnail = $t_attrs['url']; 
## thumb above is fine, but I want the largest of all from ytube
## I must do this to get to the 0.jpg
$thumbnail = substr($thumbnail, 0,-4);
## now I have my big thumbs I can use it by
$thumbs = $thumbnail.".jpg";
## we need to get the title and description of the videos..there are more options here, but I will only cover title descriptions
$yt = $yt_media ->children('http://gdata.youtube.com/schemas/2007');
$title = $yt_media->group->title;
## this is optional but it is ok if will be stored in database. We need to clean the title a littel. You need to sanitize them throughly for db.
$title = preg_replace('/[^a-zA-Z0-9 ]/s', ' ', $title);
## we want a shorter title, but you can always show the full title by removing the codes below
$title =(substr($title, 0,20)).'...';
## let's pick up description
$description = $yt_media->group->description;
## some video descriptions on youtube are extremely long, so maybe we can truncate them to 50 characters, but it is all up to you.
$description =(substr($description, 0,50)).'...';
$description = str_replace( "\n", "", $description );
## do a minor cleaning on description
$description = preg_replace('/[^a-zA-Z0-9 ]/s', ' ', $description);
## if we want to pick the video keywords, we can do so by doing so
$tags = $yt_media->group->keywords;
$tags = preg_replace('/[^a-zA-Z0-9 ]/s', '', $tags);
## some tags are extremely long
$tags =(substr($tags, 0,20)).'...';
## let's pull the publishers username.. we must always give credit to the publishers
$credit = $veedeoo->author->name;

## let's show all the thumb in <li>
## you can put form or whatever means of processing to be able to import these videos.
?>
<!-- you can style them in separate css file to fit your needs -->
<li style="display: inline; float: left; padding:5px; width:165px;" >
<img width="100" height="100" src="<?=$thumbs?>" /><br/>
<b>Title: </b> <?=$title?><br/>
<b>Description: </b> <?=$description?><br/>
<b>Tags: </b> <b><?=$tags?></b><br/>
</li>
<?
}

?>

When using the script above... just correct my improper used of php shorthand

<?=$thumbs?>

should be

<?php echo $thumbs; ?>

Sometimes, I am posessed by AERGIA the goddess of laziness :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.