Tuesday, September 11, 2007

Simple YouTube API Class in PHP: Redeaux

Good evening, people of the corn. I am absolutely certain that the grand majority of you have been waiting in the wings for the latest installment of WaxJelly's now famous YouTube API class. Thanks for waiting. In the wings. Wherever they are.


We're starting off slow again on this one, so I'll warn you, all the functionality may not quite be there yet, but this class is destined to continue to improve and become more simple to implement and use. Howeve, at the moment, using this thing is about as easy as falling off a bike.


Let's get started. First, let's talk about what you'll need in order to implement this class.



  • A YouTube API Developer's Key

  • A server running PHP5

  • 20 minutes or so

  • Popcorn

  • Red Hair

  • I could go on, but these stopped being true at bullet point #3


This is an example of what your end result should look like:


WaxJelly YouTube API Class


Now, let's talk about the code to actually make this class work for you. You'll find the following code in the index.php file included in the downloadable package at the end of this post.


Include the youtube class and instantiate it with the object $yt


include('youtube.class.php');

$yt = new youtube('YOUR_API_KEY');


…set the amount of results per page, in this case, some arbitrary number like 19 should work. The default is 25, so if you don't set this at all, that's what you'll get


$yt->set('per_page', 19);


…get videos by a tag (default search string is “mutemath” in this case ,you can change it to whatever you want.


$res = $yt->videos('mutemath');


…build the video list array


$video_list = $res['video_list']['video'];


Note that this class sets a whole buncha CONSTANTS that you can use in your script to display lots of data. Here's a list of the constants that are available:



  • YOUTUBE_PREV_PAGE = the current page -1

  • YOUTUBE_NEXT_PAGE = the current page +1

  • YOUTUBE_PAGE = the current page

  • YOUTUBE_TOTAL_PAGES = total pages in the search results

  • YOUTUBE_VIDEO_ID = the currently selected video's unique ID (for display purposes, or in the future, to call youtube's get_details method an retrieve further information, like actual comments by users on this video)

  • YOUTUBE_VIDEO_INDEX = the currently selected video's index in the video_list array (to get all the data from the array)


Later in the file, we loop through our $video_list array and format to display the videos. We do this like so:


…SETUP PAGINATION TO DISPLAY THE NEXT AND PREVIOUS PAGES OF RESULTS, AS WELL AS THE CURRENT PAGE, AND THE TOTAL NUMBER OF PAGES


<div class=”paginate”>

<a href=”?page=<?=YOUTUBE_PREV_PAGE ?>”><< prev</a>

:: page <?=YOUTUBE_PAGE ?> of <?=YOUTUBE_TOTAL_PAGES ?> ::

<a href=”?page=<?=YOUTUBE_NEXT_PAGE ?>”>next >></a>

</div>


…BUILD A TABLE OF RESULTS, LOOPING THROUGH 3 COLUMNS ON EACH ROW


<table>

<tr>

<?php


…loop through each video in the list and display it for design purposes


$i = 0;

foreach ($video_list as $k => $v) {


…limit the title's length so it doesn't break the design


$title = substr($v['title'], 0, 15) . ‘…';

echo “<td class='thumb'>

<a href='?video_id={$v['id']}'><img src='{$v['thumbnail_url']}'></a><br />

<span class='title_short'>{$title}</span><br />

<span class='length_short'>{$v['length_seconds']}</span>

<span class='rating_short'>{$v['rating_avg']} / {$v['rating_count']}</span>

</td>”;

$i++;


…only 3 videos per row


if ($i == 3) {

$i = 0;

echo ‘</tr><tr>';

}

}

?>

</tr>

</table>


Finally, we setup the display on another part of the page to actually play a video we've selected, and display the rest of the data we've gotten on that video (without using the get_details method from the api)


…first, we setup the title of the current video.


<h2>

<?=$video_list[YOUTUBE_VIDEO_INDEX]['title'] ?>

</h2>


Next, we setup the flash object call from the standard youtube format, and pass it the value for the video id


<object width=”425″ height=”350″>

<param name=”movie” value=”http://www.youtube.com/v/<?=YOUTUBE_VIDEO_ID ?>”></param>

<param name=”wmode” value=”transparent”></param>

<embed src=”http://www.youtube.com/v/<?=YOUTUBE_VIDEO_ID ?>” type=”application/x-shockwave-flash” wmode=”transparent” width=”425″ height=”350″></embed>

</object>


We can also display all the extra information, like video description, and the total number of views, comments, and the list of tags (all of which are passed to you through our youtube class in the current index of the $video_list array


<div id=”description”>

<?=$video_list[YOUTUBE_VIDEO_INDEX]['description'] ?>

</div>

<div class=”stats”>Views: <span class=”statVal”><?=$video_list[YOUTUBE_VIDEO_INDEX]['view_count'] ?></span><br />

Comments: <span class=”statVal”><?=$video_list[YOUTUBE_VIDEO_INDEX]['comment_count'] ?></span><br />

Tags: <span class=”statVal”><?=$yt->tags_for_video($video_list[YOUTUBE_VIDEO_INDEX]['tags']) ?></span><br />

</div>


That's it for now, we're going to write a PHP4 version of the class later on today, so, be ready ye' oldschoolers.


Download the source files here: ZIP | RAR

View a working version HERE


Peace love and peace.

No comments:

About Me

Ordinary People that spend much time in the box
Powered By Blogger