mhendleman
mhendleman

Reputation: 55

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'm new to php but i've followed advice and gotten my hands as filthy with it as possible. Unfortunately now I'm a little stumped with this simple Youtube app i'm trying to create.

I know there are a several related questions out there, but i haven't yet found a comprehensive solution to my problem.

Anyway, what I'm trying to do is get the urls of videos in a youtube channel, extract the video ID and create an array that i can then pass off to a javascript function for some cool client-side stuff.

Here is my code so far. I'm pretty sure that my issue relates to arrays vs strings and variables in and out of methods. In any case, my array_map function is not working and the showFullFeed() function is only returning one value instead of the array of links.

Any help is much appreaciated. Cheers

 

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);

?>

Upvotes: 2

Views: 8569

Answers (3)

Darren
Darren

Reputation: 76

This works. Not I changed the stream from favorites to videos that have been uploaded by the channel, so you may want to change that back.

First, the code for the youtube functions, I put this in a separate php file and imported it, just to keep things tidier.

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

Next 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);

This gets the IDs and stores them in an array using the array map function.

Thanks for posting the code, I was looking for something to do what this does, so it's helped me out a lot.

Upvotes: 6

M.Ali El-Sayed
M.Ali El-Sayed

Reputation: 1779

daer just replace

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

to

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

Upvotes: 1

Artefacto
Artefacto

Reputation: 97845

There are probably other problems, but you are not giving array_map a valid callback. Also, your callback function should receive at least one argument.

Upvotes: 0

Related Questions