Looking for a function to get 3 youtube thumbnail + videos??

Discussion in 'PHP' started by 123GoToAndPlay, Feb 25, 2010.

  1. #1
    Hi,

    I am looking for a php function which gets 3 youtube thumbnails based on the ?v= value from youtube
    ??
     
    123GoToAndPlay, Feb 25, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <?php
    /*
    @Function: YouTube Thumbnail Fetcher
    @Author: Dan Mcdonald.
    @Date: 21/05/2009
    */
    function get_thumbs($youtube_url){
    $youtube_url = trim(str_replace(array("http://", "www."), "", strtolower($youtube_url)));
    if(preg_match("/youtube\.com\/watch\?v=([a-z0-9]*)/i", $youtube_url)){
    preg_match_all("/http:\/\/i1\.ytimg\.com\/vi\/([A-Za-z0-9]*)\/default\.jpg/", file_get_contents("http://www.".$youtube_url), $thumbs);
    return print_r($thumbs[0]);
    } else {
    return false;
    }
    }
    
    get_thumbs("http://www.youtube.com/watch?v=dPdTAg_krJI");
    ?>
    PHP:
    Enjoy ;)
     
    danx10, Feb 25, 2010 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thx,
    hmm

    with $test = getThumbs i get
    
    Array
    (
        [0] => http://i1.ytimg.com/vi/lHjNmyzrVvM/default.jpg
        [1] => http://i1.ytimg.com/vi/lHjNmyzrVvM/default.jpg
        [2] => http://i1.ytimg.com/vi/DwzakwE2axI/default.jpg
        [3] => http://i1.ytimg.com/vi/DwzakwE2axI/default.jpg
        [4] => http://i1.ytimg.com/vi/p7gjK3OgUM0/default.jpg
        [5] => http://i1.ytimg.com/vi/p7gjK3OgUM0/default.jpg
        [6] => http://i1.ytimg.com/vi/pUr6nN8YkDU/default.jpg
        [7] => http://i1.ytimg.com/vi/pUr6nN8YkDU/default.jpg
        [8] => http://i1.ytimg.com/vi/HthL62uI3ZU/default.jpg
        [9] => http://i1.ytimg.com/vi/HthL62uI3ZU/default.jpg
        [10] => http://i1.ytimg.com/vi/pUr6nN8YkDU/default.jpg
        [11] => http://i1.ytimg.com/vi/pUr6nN8YkDU/default.jpg
    )
    
    
    Code (markup):
    how would i get say the 3
    $test[3]???

    btw i am looking for the last 3 video i uploaded or my favorites
     
    Last edited: Feb 25, 2010
    123GoToAndPlay, Feb 25, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    This example would return only 3 thumbnails, and then you can select a thumbnail like $thumbs[2] (to get the last thumbnail from the 3 returned)

    <?php
    /*
    @Function: YouTube Thumbnail Fetcher
    @Author: Dan Mcdonald.
    @Date: 21/05/2009
    */
    function get_thumbs($youtube_url){
    $youtube_url = trim(str_replace(array("http://", "www."), "", strtolower($youtube_url)));
    if(preg_match("/youtube\.com\/watch\?v=([a-z0-9]*)/i", $youtube_url)){
    preg_match_all("/http:\/\/i1\.ytimg\.com\/vi\/([A-Za-z0-9]*)\/default\.jpg/", file_get_contents("http://www.".$youtube_url), $thumbs);
    return array_slice($thumbs[0], 0, 3);
    } else {
    return false;
    }
    }
    
    $thumbs = get_thumbs("http://www.youtube.com/watch?v=dPdTAg_krJI");
    
    echo $thumbs[2];
    ?>
    PHP:
    This example would return all thumbnails, and then you can select a thumbnail like $thumbs[3] (to get the third thumbnail)

    <?php
    /*
    @Function: YouTube Thumbnail Fetcher
    @Author: Dan Mcdonald.
    @Date: 21/05/2009
    */
    function get_thumbs($youtube_url){
    $youtube_url = trim(str_replace(array("http://", "www."), "", strtolower($youtube_url)));
    if(preg_match("/youtube\.com\/watch\?v=([a-z0-9]*)/i", $youtube_url)){
    preg_match_all("/http:\/\/i1\.ytimg\.com\/vi\/([A-Za-z0-9]*)\/default\.jpg/", file_get_contents("http://www.".$youtube_url), $thumbs);
    return $thumbs[0];
    } else {
    return false;
    }
    }
    
    $thumbs = get_thumbs("http://www.youtube.com/watch?v=dPdTAg_krJI");
    
    echo $thumbs[3];
    ?>
    PHP:
     
    Last edited: Feb 25, 2010
    danx10, Feb 25, 2010 IP
  5. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ah tx,

    Still a couple of questions:
    1/ when getting thumbs for http://www.youtube.com/watch?v=dPdTAg_krJI each time i refresh the thumb is different?? (array_slice??)
    2/ I will need title and description as well
    3/Is it possible to get a feed based on username (i am now testing with simplepie )? Which i would use to get the thumbs?


    ps: did you see ufc 110 nog velasquez
     
    123GoToAndPlay, Feb 25, 2010 IP
  6. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hmm, still haven't found a solution

    i want this layout
    [​IMG]

    but i stuck with this
    .bldd.nl/youtube/

    For this i am using youtube api with javascript GSvideoBar but i can't seem to find the title / description
     
    123GoToAndPlay, Mar 17, 2010 IP
  7. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok found a solution
    here's the result:
    .bldd.nl/youtube/index4.php
     
    123GoToAndPlay, Mar 17, 2010 IP