Get video links from Youtube Channel, extract video IDs from Urls and store them in an array
This is my first question on this site. I am new to php but I followed the advice and got my hands as messy as possible. Unfortunately, now I'm a little bit stumped with this simple Youtube app I'm trying to create.
I know there are several related questions, but I have not yet found a comprehensive solution to my problem.
Anyway, what I'm trying to do is get the urls of the videos in the YouTube channel, extract the video id, and create an array that I can then pass to a javascript function for some cool client apps.
Here is my code. I'm pretty sure my problem is with arrays and strings and variables in and out of methods. Anyway, my array_map function is not working and showFullFeed () function only returns one value instead of an array of references.
Any help is very much. Greetings
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 the YouTube video IDs.
// The identity string starts after "v =", which is usually executed right after // "youtube.com/watch?" in the url $ idStarts = strpos ($ ytURL, "? v =");
// In case "v =" is not correct after "?" (not likely, but I like to keep me // basics covered) this will be after "&": if ($ idStarts === FALSE) $ idStarts = strpos ($ ytURL, "& v ="); // If still FALSE, the URL has no vid if ($ idStarts === FALSE) die ("The YouTube video ID was not found. Please double check your URL.");
// Offset starting location to match the start of the identifier 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);
? >
a source to share
It works. I didn't change the stream from favorites to videos that were uploaded by the channel, so you can change it.
First, the code for the youtube functions, I put this in a separate php file and imported it to keep things tidy.
<?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 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;
}
?>
Following is the code in the main file that calls it
require_once('youtube_channelfeed.php');
$youtube = new ChannelFeed('username');
$vids = $youtube->showFullFeed();
$vidIDs = array_map("getYouTubeID",$vids);
Retrieves identifiers and stores them in an array using the array display function.
Thanks for submitting the code, I was looking for something to do so it helped me a lot.
a source to share
There are probably other problems, but you are not giving array_map a valid callback . Also, your callback function must receive at least one argument.
a source to share