How to get the id of the latest uploaded video in

2020-02-26 02:21发布

In youtube How do i get the id of the latest uploaded video (the one that comes in the url like v=....) of a channel to which Im subscribed, for embeded
im using php in my server side

标签: php youtube
4条回答
▲ chillily
2楼-- · 2020-02-26 02:57

As of April 20, 2015, it looks like the answer mentioned above may no longer work. Here's an example using a YouTube Channel ID (can be found in the source of a Channel page.)

<?php

$id = NULL;
$channel_id = 'someChannelID';

$xml = simplexml_load_file(sprintf('https://www.youtube.com/feeds/videos.xml?channel_id=%s', $channel_id));

if (!empty($xml->entry[0]->children('yt', true)->videoId[0])){
    $id = $xml->entry[0]->children('yt', true)->videoId[0];
}

echo $id; // Outputs the video ID.
查看更多
趁早两清
3楼-- · 2020-02-26 03:06

Here's a example using the YouTube RSS feeds, simplexml_load_file, parse_url, and parse_str.

<?php

$id = NULL;
$username = 'YouTube';

$xml = simplexml_load_file(sprintf('http://gdata.youtube.com/feeds/base/users/%s/uploads?alt=rss&v=2&orderby=published', $username));

if ( ! empty($xml->channel->item[0]->link) )
{
  parse_str(parse_url($xml->channel->item[0]->link, PHP_URL_QUERY), $url_query);

  if ( ! empty($url_query['v']) )
    $id = $url_query['v'];
}

echo $id; // Outputs the video ID.
查看更多
兄弟一词,经得起流年.
4楼-- · 2020-02-26 03:10

Same here as with Greg Brendel. YT did make some changes: "As we upgrade the YouTube Data API to bring more features, we’ll begin shutting down the old version on April 20, 2015. This will result in the current YouTube app not working on certain device models from 2012 and older."

https://support.google.com/youtube/answer/6098135?p=yt_devicesupport&hl=en&rd=1

查看更多
对你真心纯属浪费
5楼-- · 2020-02-26 03:11

Here's how to do it via the YouTube API once you set up your API Key.

<?php

$channel_id = 'someChannelId';
$api_key = 'yourAPIKey';

$json_url="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=".$channel_id."&key=".$api_key;
$json = file_get_contents($json_url);
$listFromYouTube=json_decode($json);
$id = $listFromYouTube->items[0]->snippet->resourceId->videoId;

echo $id; // Outputs the video ID.

Generating your API Key:

  1. Goto - https://console.developers.google.com
  2. Create a new project
  3. Select "APIs & auth"
  4. Select "APIs"
  5. Select "Youtube Data API v3" and enable it
  6. Select "Credentials"
  7. Create new Key, select browser and then press create (don't enter anything in the text box)
查看更多
登录 后发表回答