Getting title and description of embedded YouTube video

后端 未结 7 1631
抹茶落季
抹茶落季 2021-02-05 12:36

On a site I\'m developing I embed videos from YouTube and want to get the video title and its description.

How do I get that information?

相关标签:
7条回答
  • 2021-02-05 12:51

    I read this topic a bit in delay. I did something like this using jSON and YT API's

    $json = json_decode( file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$rs['vid']."?v=2&prettyprint=true&alt=jsonc") );
    

    Note: $rs['vid'] is the video ID dinamically retrived from my DB.

    Once you put the contents in the handle $json you can retrive like this:

    $json->data->description;
    $json->data->title;
    

    use var_dump( $json ) to view all values you can access.

    0 讨论(0)
  • 2021-02-05 12:56

    I'd start by taking a look at Youtube Data API to get what you want: http://code.google.com/apis/youtube/getting_started.html#data_api

    0 讨论(0)
  • 2021-02-05 12:58

    GData is deprecated, but one can still get the video description by calling this endpoint:

    https://www.googleapis.com/youtube/v3/videos?part=snippet&id=[video_id]&key=[api_key]

    It will return a response of the form:

    {
     "kind": "youtube#videoListResponse",
     "etag": "\"...\"",
     "pageInfo": {
      "totalResults": 1,
      "resultsPerPage": 1
     },
     "items": [
      {
       "kind": "youtube#video",
       "etag": "\"...\"",
       "id": "...",
       "snippet": {
        "publishedAt": "...",
        "channelId": "...",
        "title": "...",
        "description": "...",
        "thumbnails": { ... },
        "channelTitle": "...",
        "tags": [ ... ],
        "categoryId": "...",
        "liveBroadcastContent": "...",
        "localized": {
         "title": "...",
         "description": "..."
        },
        "defaultAudioLanguage": "..."
       }
      }
     ]
    }
    

    The description can be found at items.localized.description.

    0 讨论(0)
  • 2021-02-05 13:03

    gdata is no longer available

    you can use the following instead

    https://www.googleapis.com/youtube/v3/videos?part=snippet&id=(Video_ID)&key=(API_Key)

    0 讨论(0)
  • 2021-02-05 13:06

    To get the DESCRIPTION element, you need to access the gdata version of the video's info, and you can return json using alt=json on the path. In this case, oHg5SJYRHA0 is the video ID, found at the end of the url of the video you're working with on YouTube, e.g. www.youtube.com/watch?v=oHg5SJYRHA0

    http://gdata.youtube.com/feeds/api/videos/oHg5SJYRHA0?v=2&alt=json&prettyprint=true

    (the prettyprint is formatting to make that easy to read, you don't need it for what you're doing)

    You can grab the JSON, add it into a variable and access it using jQuery:

    var youTubeURL = 'http://gdata.youtube.com/feeds/api/videos/oHg5SJYRHA0?v=2&alt=json';
    var json = (function() {
        var json = null;
        $.ajax({
            'async': false,
            'global': false,
            'url': youTubeURL,
            'dataType': "json",
            'success': function(data) {
                json = data;
            }
        });
        return json;
    })();
    

    Then access it using object notation:

    alert("Title: " + json.entry.title.$t +"\nDescription:\n " + json.entry.media$group.media$description.$t + "\n");
    
    0 讨论(0)
  • 2021-02-05 13:12

    Youtube API V2.0 has been deprecated. It shows some wrong value for title "youtube.com/devicesupport" . pLease switch on to API V3.0

    YOu can refer the following PHP code and modify yours in js or jquery as per your needs..

    function youtube_title($id) {
     $id = 'YOUTUBE_ID';
    // returns a single line of JSON that contains the video title. Not a giant request.
    $videoTitle = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$id."&key=YOUR_API_KEY&fields=items(id,snippet(title),statistics)&part=snippet,statistics");
    // despite @ suppress, it will be false if it fails
    if ($videoTitle) {
    $json = json_decode($videoTitle, true);
    
    return $json['items'][0]['snippet']['title'];
    } else {
    return false;
    }
    }
    

    update:

    Jquery code to get the title-

     $.getJSON('https://www.googleapis.com/youtube/v3/videos?id={VIDEOID}&key={YOUR API KEY}&part=snippet&callback=?',function(data){
        if (typeof(data.items[0]) != "undefined") {
            console.log('video exists ' + data.items[0].snippet.title);
           } else {
            console.log('video not exists');
         }   
        });
    
    0 讨论(0)
提交回复
热议问题