从Youtube频道获取视频链接,从Urls中提取视频ID并将其存储在数组中

mhe*_*man 2 php youtube arrays api gdata

这是我在这个网站上的第一个问题.我是php的新手但是我已经按照建议尽可能地弄脏了它.不幸的是现在我有点难过这个简单的Youtube应用程序,我正在努力创建.

我知道有几个相关的问题,但我还没有找到一个全面的解决方案来解决我的问题.

无论如何,我要做的是在youtube频道中获取视频的网址,提取视频ID并创建一个数组,然后我可以传递给javascript函数以获得一些很酷的客户端内容.

到目前为止,这是我的代码.我很确定我的问题与数组与字符串以及方法内外的变量有关.在任何情况下,我的array_map函数都不起作用,showFullFeed()函数只返回一个值而不是链接数组.

任何帮助都是很有帮助的.干杯

 

class ChannelFeed {

function __construct($username) { $this->username=$username; $this->feedUrl=$url='http://gdata.youtube.com/feeds/api/users/'.$username.'/favorites'; $this->feed=simplexml_load_file($url); }

public function getYTid() {

$ytURL = $this->feed->entry->link['href'];

$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;
} public function showFullFeed() { foreach($this->feed->entry as $video){ return $vidarray[] = $video->link['href']; } } }; $youtube = new ChannelFeed('username'); $vids = $youtube->showFullFeed(); $vidIDs = array_map(getYTid(),$vids);

?>

小智 6

这有效.我没有将流从收藏夹更改为频道上传的视频,因此您可能希望将其更改回来.

首先,youtube函数的代码,我把它放在一个单独的php文件中并导入它,只是为了让事情更加整洁.

<?php
  class ChannelFeed
  {
    function __construct( $username )
    {
        $this->username=$username;
        $this->feedUrl=$url='http://gdata.youtube.com/feeds/api/users/'.$username.'/uploads';
        $this->feed=simplexml_load_file($url);
    }

    public function showFullFeed()
    { 
        $vidarray = array();

        foreach($this->feed->entry as $video)
        {
            $vidURL = (string)$video->link['href'];
            $vidarray[] = $vidURL;
        }

        return $vidarray;
}
}

function getYouTubeID($vidURL)
{
    $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($vidURL, "?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($vidURL, "&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($vidURL, $idStarts, $ytvIDlen);    

    return $ytvID;   
}
?>
Run Code Online (Sandbox Code Playgroud)

接下来是调用它的主文件中的代码

require_once('youtube_channelfeed.php');

$youtube = new ChannelFeed('username');
$vids = $youtube->showFullFeed();
$vidIDs = array_map("getYouTubeID",$vids);
Run Code Online (Sandbox Code Playgroud)

这将获取ID并使用数组映射函数将它们存储在数组中.

感谢您发布代码,我正在寻找能够做到这一点的事情,所以这对我帮助很大.