How can I retrieve YouTube video details from video URL using PHP?

前端 未结 9 644
南方客
南方客 2020-12-02 13:46

Using PHP, how can I get video information like title, description, thumbnail from a youtube video URL e.g.

http://www.youtube.com/watch?v=B4CRkpBGQzU


        
相关标签:
9条回答
  • 2020-12-02 13:54

    Yet another URL API that can be helpful is: https://www.youtube.com/get_video_info?video_id=B4CRkpBGQzU

    video_id is "v" argument of youTube. Result is a dictionary in URL-encoded format (key1=value1&key2=value2&...)

    This is undocumented API existing for long time, so exploring it is up to developer. I am aware of "status" (ok/fail), "errorcode" (100 and 150 in my practice), "reason" (string description of error). I am getting duration ("length_seconds") this way because oEmbed does not provide this information (strange, but true) and I can hardly motivate every employer to get keys from youTube to use official API

    0 讨论(0)
  • 2020-12-02 13:58

    Here is a function that can return several pieces of information. I included some sample properties, but others are available:

    <?php
    extension_loaded('openssl') or die('openssl');
    
    function youtube_info(string $id_s): object {
       $info_s = 'https://www.youtube.com/get_video_info?video_id=' . $id_s;
       $get_s = file_get_contents($info_s);
       parse_str($get_s, $get_m);
       $resp_s = $get_m['player_response'];
       return json_decode($resp_s)->microformat->playerMicroformatRenderer;
    }
    
    $o = youtube_info('2zPxY5WGG1E');
    
    var_dump(
       $o->title->simpleText,
       $o->description->simpleText,
       $o->thumbnail->thumbnails[0]->url
    );
    

    https://github.com/89z/sienna/blob/a5ba9109/youtube/youtube.php

    0 讨论(0)
  • 2020-12-02 14:03

    function to get video id from video url

    function getYTid($ytURL) {
    
        $ytvIDlen = 11; // This is the length of YouTube's video IDs
    
        // The ID string starts after "v=", which is usually right after 
        // "youtube.com/watch?" in the URL
        $idStarts = strpos($ytURL, "?v=");
    
        // In case the "v=" is NOT right after the "?" (not likely, but I like to keep my 
        // bases covered), it will be after an "&":
        if($idStarts === FALSE)
            $idStarts = strpos($ytURL, "&v=");
        // If still FALSE, URL doesn't have a vid ID
        if($idStarts === FALSE)
            die("YouTube video ID not found. Please double-check your URL.");
    
        // Offset the start location to match the beginning of the ID string
        $idStarts +=3;
    
        // Get the ID string and return it
        $ytvID = substr($ytURL, $idStarts, $ytvIDlen);
    
        return $ytvID;
    
    }
    

    and then

    $video = getYTid($videourl);
    $video_feed = file_get_contents("http://gdata.youtube.com/feeds/api/videos/$video");
                $sxml = new SimpleXmlElement($video_feed);
    
                //set up nodes
                $namespaces = $sxml->getNameSpaces(true);
                $media = $sxml->children($namespaces['media']);
                $yt = $media->children($namespaces['yt']);
                $yt_attrs = $yt->duration->attributes();
    
                //vars
                 $video_title = $sxml->title;
    
                 $video_description = $sxml->content;
    
                 $video_keywords = $media->group->keywords;
    
                 $video_length = $yt_attrs['seconds'];
    
    0 讨论(0)
  • 2020-12-02 14:04

    I know this is an old question but I wrote a package for this purpose, I will put it here maybe someone needs this.

    composer require smoqadam/youtube-video-info:dev-master

    $details = $video->getDetails();
    echo $details->getVideoId();
    echo $details->getTitle();
    echo $details->getThumbnails();
    echo $details->getViewCount();
    echo $details->getRating();
    

    more info in here

    0 讨论(0)
  • 2020-12-02 14:05

    You can get data from youtube oembed interface in two formats: XML and JSON

    Interface address: http://www.youtube.com/oembed?url=youtubeurl&format=json

    Use this PHP function to get data

     function get_youtube($url){
    
     $youtube = "http://www.youtube.com/oembed?url=". $url ."&format=json";
    
     $curl = curl_init($youtube);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     $return = curl_exec($curl);
     curl_close($curl);
     return json_decode($return, true);
    
     }
    
    $url = // youtube video url 
    
    // Display Data 
    print_r(get_youtube($url));
    

    Don't forget to enable extension=php_curl.dll in your php.ini

    0 讨论(0)
  • 2020-12-02 14:08

    To get youtube video description, different sized thumbnail, tags, etc use updated v3 googleapis

    Sample URL: https://www.googleapis.com/youtube/v3/videos?part=snippet&id=7wtfhZwyrcc&key=my_google_api_key

    $google_api_key = 'google_api_key';
    $video_id = 'youtube_video_id';
    $api_url = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&id='.$video_id.'&key='.$google_api_key;
    $json = file_get_contents($api_url);
    $obj = json_decode($json);
    var_dump($obj);
    
    0 讨论(0)
提交回复
热议问题