Get the Youtube Video Thumbnail from Youtube video Url using PHP

后端 未结 5 1284
星月不相逢
星月不相逢 2021-02-06 15:26

Lets say I have a youtube video url www.youtube.com/watch?v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2

I want to get the video thumbnail ->

相关标签:
5条回答
  • 2021-02-06 15:55

    To extract the identifier from the following URL :

    $url = 'www.youtube.com/watch?v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2';
    


    You can first use parse_url() to get the query string :

    $queryString = parse_url($url, PHP_URL_QUERY);
    var_dump($queryString);
    

    Which, here, would give you :

    string 'v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2' (length=49)
    


    And, then, use parse_str() to extract the parameters from that query string :

    parse_str($queryString, $params);
    var_dump($params);
    

    Which would get you the following array :

    array
      'v' => string 'B4CRkpBGQzU' (length=11)
      'feature' => string 'youtube_gdata' (length=13)
      'par1' => string '1' (length=1)
      'par2' => string '2' (length=1)
    


    And, now, it's just a matter of using the v item from that array, injecting it into the thumbnail URL :

    if (isset($params['v'])) {
        echo "i3.ytimg.com/vi/{$params['v']}/default.jpg";
    }
    

    Which gives :

    i3.ytimg.com/vi/B4CRkpBGQzU/default.jpg
    
    0 讨论(0)
  • 2021-02-06 15:56

    Assuming youtube doesn't change their format:

    $thumb = preg_match('/watch\?v=(.*)&feature/',$url, $matches);

    This will get you the 'B4CRkpBGQzU' part in $matches[1];

    0 讨论(0)
  • 2021-02-06 15:59

    Getting the video ID from the URL

    $url = "http://www.youtube.com/watch?v=oHg5SJYRHA0&feature=relate";
    parse_str( parse_url( $url, PHP_URL_QUERY ) );
    echo $vidID;//this is video id
    

    Getting the images

    $thumb1 = file_get_contents("http://img.youtube.com/vi/$vidID/0.jpg");
    $thumb2 = file_get_contents("http://img.youtube.com/vi/$vidID/1.jpg");
    $thumb3 = file_get_contents("http://img.youtube.com/vi/$vidID/2.jpg");
    $thumb4 = file_get_contents("http://img.youtube.com/vi/$vidID/3.jpg");
    
    0 讨论(0)
  • 2021-02-06 16:00
    <?php
            function getYoutubeImage($e){
            //GET THE URL
            $url = $e;
    
            $queryString = parse_url($url, PHP_URL_QUERY);
    
            parse_str($queryString, $params);
    
            $v = $params['v'];  
            //DISPLAY THE IMAGE
            if(strlen($v)>0){
                echo "<img src='http://i3.ytimg.com/vi/$v/default.jpg' width='150' />";
            }
        }
    ?>
    
    <?php
        getYoutubeImage("http://www.youtube.com/watch?v=CXWSlho1mMY&feature=plcp");
    ?>
    
    0 讨论(0)
  • 2021-02-06 16:00

    https://img.youtube.com/vi/B4CRkpBGQzU/default.jpg https://img.youtube.com/vi/B4CRkpBGQzU/1.jpg https://img.youtube.com/vi/B4CRkpBGQzU/2.jpg https://img.youtube.com/vi/B4CRkpBGQzU/3.jpg

    $url = "http://youtube.com/watch?v=B4CRkpBGQzU";
    $url = explode("&", $url);
    $vidID = preg_match("|?v=(.*)|", $url);
    $thumb_default = file_get_contents("http://img.youtube.com/vi/$vidID/default.jpg");
    
    $thumb1 = file_get_contents("http://img.youtube.com/vi/$vidID/0.jpg");
    $thumb2 = file_get_contents("http://img.youtube.com/vi/$vidID/1.jpg");
    $thumb3 = file_get_contents("http://img.youtube.com/vi/$vidID/2.jpg");
    $thumb4 = file_get_contents("http://img.youtube.com/vi/$vidID/3.jpg");
    
    0 讨论(0)
提交回复
热议问题