1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Get a YouTube video URL in PHP

Discussion in 'PHP' started by WPC, Nov 20, 2011.

  1. #1
    Quick piece of code that should, in theory, fetch the FLV/MP4/whatever URL of the video, given a youtube URL.


    The example has a hard-coded url, turn it into a function to make it a parameter instead.


    Also, it has absolutely no error checking, so you may wanna implement that (e.g. you should check if $r returns false and, if so, throw an error and die).


    $c = curl_init();curl_setopt($c, CURLOPT_URL, 'http://www.youtube.com/watch?v=sRLz4aDCAs0');curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);curl_setopt($c, CURLOPT_FAILONERROR, 1);$r = curl_exec($c);curl_close($c);
    preg_match_all('#url_encoded_fmt_stream_map=([^&]+)#', $r, $fvars);$fvars = explode('&', urldecode($fvars[1][0]));
    $results = array();$resultIndex = -1;
    foreach($fvars as $item){    if(strpos($item, 'url=') !== false)        $resultIndex++;    if(!isset($results[$resultIndex]))        $results[$resultIndex] = array();    if(strpos($item, 'url=') === false)    {        $parse = explode('=', $item);        $results[$resultIndex][$parse[0]] = urldecode($parse[1]);        continue;    }    $parse = preg_match('#url=(.+)$#', $item, $url);    $results[$resultIndex]['url'] = urldecode($url[1]);}
    var_dump($results);
    PHP:

    This results in an array containing arrays like so:


    
      array(5) {
        ["url"]=>
        string(419) "some_youtube_url"
        ["quality"]=>
        string(5) "small"
        ["fallback_host"]=>
        string(26) "tc.v4.cache5.c.youtube.com"
        ["type"]=>
        string(11) "video/x-flv"
        ["itag"]=>
        string(1) "5"
      }
    
    Code (markup):

    Just an example anyway, there may be an easier way to do it but this seemed logical at the time.
     
    WPC, Nov 20, 2011 IP
  2. rcajht

    rcajht Peon

    Messages:
    103
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I am looking for this, thanks for sharing.
     
    rcajht, Nov 21, 2011 IP
  3. gUs aUL

    gUs aUL Greenhorn

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    thanks brother....good job....
     
    gUs aUL, Nov 21, 2011 IP
  4. kentIT

    kentIT Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    but if a link youtube get file hd that it is file wmv, how can you get it
     
    kentIT, Nov 21, 2011 IP
  5. kentIT

    kentIT Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank WPC post code example, i'll post code check file HD if it exist or not exist get file flv
    
    function getYouTube($url)
    {    
    $geturl = NULL;    
    $c = curl_init();    
    curl_setopt($c, CURLOPT_URL,$url);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_FAILONERROR, 1); 
    $err = curl_error($c);
    $r = curl_exec($c);
    curl_close($c);
    if(isset($err))
    {
    $geturl = "Link Warn Video Not Found";    
    }else{   
     preg_match_all('#url_encoded_fmt_stream_map=([^&]+)#', $r, $fvars);    
    $fvars = explode('&', urldecode($fvars[1][0]));        
    $results = array();    
    $resultIndex = -1;   
    foreach($fvars as $item)    
    {       
     if(strpos($item, 'url=') !== false)$resultIndex++;               
     if(!isset($results[$resultIndex]))$results[$resultIndex] = array();       
     if(strpos($item, 'url=') === false)        
    {            
    $parse = explode('=', $item);            
    $results[$resultIndex][$parse[0]] = urldecode($parse[1]);            
    continue;        
    }                
    $parse = preg_match('#url=(.+)$#', $item, $url);        
    $results[$resultIndex]['url'] = urldecode($url[1]);    
    }
    //video/mp4; codecs="avc1.42001E, mp4a.40.2"    
    $searchType = "/mp4/i";    
    foreach($results as $test)    
    {        
    if(preg_match($searchType,$test["type"]))        
    {            
    $geturl = $test["url"];            
    echo "Type {$test["type"]} and link file {$test["url"]}";            
    break;        
    }    
    }    
    if(empty($geturl))    
    {        
    $geturl = $results[0]["url"];    
    }    
    }    
    return $geturl;
    }
    
    PHP:
    Call Function
    
    echo getYouTube("http://www.youtube.com/watch?v=sRLz4aDCAs0");
    
    PHP:
    :D
     
    kentIT, Nov 21, 2011 IP
  6. kids

    kids Active Member

    Messages:
    411
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    68
    #6
    This code can get clip embed author right?
     
    kids, Nov 21, 2011 IP
  7. kentIT

    kentIT Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This is code get url file movies let you can set embed with Flash Player like JW Payer, MP Player ...etc
     
    kentIT, Nov 22, 2011 IP
  8. kids

    kids Active Member

    Messages:
    411
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    68
    #8
    If you want to embed with JW, I think you no need so much to do like that.

    I have done with this code:
    That's enough to show youtube clip on my page.
     
    kids, Nov 22, 2011 IP
  9. kentIT

    kentIT Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    it need for working if you're want to track visits or to download that file and hide the address link movies of the file
     
    kentIT, Nov 22, 2011 IP