Youtube: получить последнее (новое) видео с канала


Вот такой небольшой код взятый из плагина к wordpress:

function getXML($channel_id){
	
	$url=sprintf('https://www.youtube.com/feeds/videos.xml?channel_id=%s', $channel_id);
	$xml="";
	$xml = @simplexml_load_file($url);
	if ($xml==""){
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$data = curl_exec($curl);
		$xml = simplexml_load_string($data);
	}
	
	return $xml;
}

$channel_id = "UCMQbo8SZEGJvPdpD8skSFYg";

if ($channel_id!=""){
	$xml = getXML($channel_id);

	if ($xml->entry[0] && !empty($xml->entry[0]->children('yt', true)->videoId[0])){
		$id = $xml->entry[0]->children('yt', true)->videoId[0];
	}
}
if ($channel_id != "" && $id != ""){
	echo '<iframe src="https://www.youtube.com/embed/'.$id.'" frameborder="0" allowfullscreen></iframe>';
}

и еще парочка вариантов:

$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.

и такой через API:

$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.