📅  最后修改于: 2022-03-11 14:57:47.342000             🧑  作者: Mango
function getYouTubeXMLUrl( $url, $return_id_only = false ) {
$xml_youtube_url_base = 'https://www.youtube.com/feeds/videos.xml';
$preg_entities = [
'channel_id' => '\/channel\/(([^\/])+?)$', //match YouTube channel ID from url
'user' => '\/user\/(([^\/])+?)$', //match YouTube user from url
'playlist_id' => '\/playlist\?list=(([^\/])+?)$', //match YouTube playlist ID from url
];
foreach ( $preg_entities as $key => $preg_entity ) {
if ( preg_match( '/' . $preg_entity . '/', $url, $matches ) ) {
if ( isset( $matches[1] ) ) {
if($return_id_only === false){
return $xml_youtube_url_base . '?' . $key . '=' . $matches[1];
}else{
return [
'type' => $key,
'id' => $matches[1],
];
}
}
}
}
}
$url = 'https://www.youtube.com/channel/UCBLAoqCQyz6a0OvwXWzKZag';
$xml_url = getYouTubeXMLUrl($url);
echo $xml_url; //outputs https://www.youtube.com/feeds/videos.xml?channel_id=UCBLAoqCQyz6a0OvwXWzKZag
$entity_id_array = getYouTubeXMLUrl($url, true);
print_r($entity_id_array); // outputs Array ( [type] => channel_id [id] => UCBLAoqCQyz6a0OvwXWzKZag );