[CODE]Extracting Video Links from YouTube Playlist

Status
Not open for further replies.

Gaurav

Active Member
641
2010
104
0
Made this since I needed to download Crysis Walkthrough and jDownloader wouldn't parse Playlists xD.

Note: If you find that a link is incomplete please fix the RegEx accordingly and PM me, I'll be more than glad to add updates ;)

Here's the Code:

PHP:
<?php

    function getVideos($link){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,  $link);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);
        curl_close($ch);
        
        preg_match_all("/\/watch\?v=[a-zA-Z0-9\-_]+/", $data, $matches);
        
        $i = 0;
        
        foreach($matches[0] as $videos){
            if($i % 2 == 0)
                echo "www.youtube.com".$videos."<br />";
            $i++;
        }
        
    }
    
    getVideos("Playlist Link here..");


?>

Do comment if you have any problems/doubts or you wish to express some gratitude :)
 
5 comments
why not use the api ?

PHP:
$url="http://gdata.youtube.com/feeds/api/videos/KHLrEF9tHjw/related";
$comments=simplexml_load_file($url);

foreach($videos->entry as $video)
{
    echo $video->title . "<br />";
}
 
Good job :)
but when a service is offering an API , the best and easy way to perform a task is by using the API.
btw ,regex is not the best way to parse html .
 
Status
Not open for further replies.
Back
Top