PHP YouTube class

I’ve written a short PHP class for importing data from YouTube - usefull for showing content related videos or a small YouTube search engine on you’re site. PHP version 5.x is needed, as the script is using PHP 5.x class SimpleXMLELement to parse the XML data returned from YouTube Data API. I won’t explain the class step-by-step, only the usage and it’s output.

1258198.jpg

Class: youtube
Function: get()

Usage:

$x = new youtube;
$x->get(QUERY STRING, OUTPUT LIMIT, HEIGHT OF EMBEDDED VIDEO, WIDTH OF EMBEDDED VIDEO);

QUERY STRING - search query (words, with which class preforms the query)
OUTPUT LIMIT - number of videos in the returned query (default = 10)
HEIGHT OF EMBEDDED VIDEO - height in pixels for the HTML code to embedd video (default = 350px)
WIDTH OF EMBEDDED VIDEO - width in pixels for the HTML code to embedd video (default = 425px)

Output:

title - title of the video
link - link to the video on YouTube
date - date stamp (when the video was published)
content - long description of the video
author - user from YouTube that uploaded the video
keywords - words that describe the video
video - url to the video file
video_duration - duration of the video file in seconds
code - HTML code to embedd the video
thumb - snapshot of the video (width 320px, height 240px)

Download:

- youtube.php.txt
- youtube.rar

Preview:

http://www.delavec.net/_youtube/
Insert the query string into the text input field, press Search button and system will return results - 10 videos from YouTube.

Komentarji

  1. Tomaz @ 11.02.08 | 19:49

    Dugg! Right on dude! I think that I’m going to use this.

  2. Denis @ 22.02.08 | 15:53

    Hi, can you give code of _youtube/?

  3. andrej @ 22.02.08 | 16:07

    It’s simple HTML form with foreach() of the returned results .. i guess you can write that by you’re own ..

  4. Denis @ 22.02.08 | 17:09

    I can’t ,
    get(”car”,”10″,”400″,”400″)as $value) {
    echo “Value: $value\n”;
    }
    foreach ($data as $value) {
    echo “Value: $value\n”;
    }
    ?>
    give me
    Value: Array
    Value: Array
    Value: Array
    Value: Array
    Value: Array
    Value: Array
    Value: Array
    Value: Array
    Value: Array
    Value: Array

    Warning: Invalid argument supplied for foreach() in /backup/flashpla/public_html/55/example.php on line 7

  5. Denis @ 22.02.08 | 17:12

    before <?php
    include_once (”youtube.php”);
    $x = new youtube();

  6. andrej @ 22.02.08 | 17:24

    $youtube = new youtube;
    $x = $youtube->get(”something”);
    foreach($x as $row) {
    echo $row['title'];
    }

    .. for other values, do var_dump($row) inside of foreach() ..

  7. Denis @ 22.02.08 | 18:07

    Thank’s thank’s thank’s

  8. andrej @ 22.02.08 | 18:56

    No problem :)

  9. Denis @ 8.04.08 | 18:00

    Andrej, how can i get 100 or more results in 1 array?
    I try:
    $full=array();
    for ($i = 1; $i get($search,$i,50,200,200);
    $full=$full+$x;
    }
    and in youtube.php
    function get($q, $limit = “10″, $i=”1″, $vh = “350″, $vw = “425″) {
    $url = implode(”&”,array(”http://gdata.youtube.com/feeds/api/videos?format=1″,
    “vq=”.urlencode($q),
    “max-results=”.intval($limit),
    “start-index=”.intval($i)));
    It’s not work:
    Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in /youtube.php:22 Stack trace: #0 /youtube.php(22): SimpleXMLElement->__construct(’Max-results val…’) #1 /example.php(14): youtube->get(’car’, 51, 50, 200, 200) #2 {main} thrown in /youtube.php on line 22

  10. Denis @ 8.04.08 | 18:03

    example.php
    “for ($i = 1; $i get($search,$i,50,200,200);
    $full=$full+$x;
    }….”

  11. andrej @ 8.04.08 | 20:05

    Try:

    $youtube = new youtube;
    $youtube->get(”YOURE-QUERY”,200);

    .. $limit (second condition in function array) in function represents the numbers of output videos ..

  12. Tea @ 6.05.08 | 12:57

    geeks ;)

  13. Delavec @ 8.05.08 | 20:10

    [...] - including photos/videos and other media into text. I’ve written a small PHP class, howto search videos on YouTube - it’s time to do the same for photo search on [...]

  14. Luca @ 7.06.08 | 4:42

    Hi this is nice job, i try to use this class and work fine, but i try to search for terms of other languages like Chinese and no found entry, but in youtube.com with the same search term I found them, I can ask why?

    and if is possible, how i can fix this class for this problem?
    thanks for all and sorry for my bad english (I’m Italian) :)

    bye

  15. Luca @ 7.06.08 | 5:07

    Hi, maybe i find the problem, the problem isen’t the script but the hosting, becouse in my localhost always work fine. sorry for before.

    I have another question, how i can set the to search in only music category?

    thanks again a sorry for my english :)

  16. andrej @ 7.06.08 | 11:04

    I would suggest reading the YouTube API (docs) for more info, to be exact the section “Category search in video feeds”:
    http://code.google.com/apis/youtube/reference.html#Category_search

    .. basically, you should change you’re core API url to something like:
    http://gdata.youtube.com/feeds/api/videos/-/music/

    That’s it :) Oh yes, “core url” is in line 7 in this class and must contain “?format=1″ on the end (for XML format).

    I might rewrite this class some day :) YouTube supports PHP serialize, which is much better for web transfer & process than XML ..

Napiši odgovor