這是我對該網站的第一個問題。我對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);?>