2010-05-17 94 views
2

這是我對該網站的第一個問題。我對PHP很陌生,但我遵循了建議,儘可能讓我的手變得骯髒。不幸的是,現在我對我試圖創建的這個簡單的Youtube應用程序有點困惑。從Youtube頻道獲取視頻鏈接,從Url中提取視頻ID並將其存儲在數組中

我知道那裏有幾個相關的問題,但我還沒有找到一個全面的解決方案來解決我的問題。

無論如何,我想要做的就是在YouTube頻道中獲取視頻的URL,提取視頻ID並創建一個數組,然後我可以傳遞給JavaScript函數以獲得一些很酷的客戶端內容。

這是我的代碼到目前爲止。我很確定我的問題涉及數組與字符串和變量的方法。無論如何,我的array_map函數不起作用,showFullFeed()函數只返回一個值而不是鏈接數組。

任何幫助都非常appreaciated。乾杯

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; 
} 
?> 

接下來是在調用它

require_once('youtube_channelfeed.php'); 

$youtube = new ChannelFeed('username'); 
$vids = $youtube->showFullFeed(); 
$vidIDs = array_map("getYouTubeID",$vids); 

此使用陣列映射函數得到的ID,並將它們存儲在數組中的主文件中的代碼。

感謝您發佈代碼,我一直在尋找能夠做到的事情,所以它幫了我很多。

0

可能有其他的問題,但你不給array_map有效callback。另外,你的回調函數應該至少接收一個參數。

1

康達爾只需更換

public function showFullFeed() 
{ 
foreach($this->feed->entry as $video){ 
return $vidarray[] = $video->link['href']; 
} 
} 

public function showFullFeed() 
{ 
foreach($this->feed->entry as $video){ 
    $vidarray[] = $video->link['href']; 
} 
     return $vidarray ; 
}