Following redirect to a second URL

Discussion in 'PHP' started by jakejarvis, Apr 22, 2007.

  1. #1
    Hi all,

    I'm developing a site relating to YouTube videos. I currently have a function that retrieves the YouTube FLV URL. For example:

    http://www.youtube.com/get_video?video_id=XC71t8GzqPs&t=OEgsToPDskJtI1jb1nE9xx9Ia-RxuCoh

    However, this URL redirects to another URL that is the true location of the video file. For example, the above URL redirects to:

    http://lax-v158.lax.youtube.com/get_video?video_id=XC71t8GzqPs

    My Flash applet does not work if the function returns the non-redirected URL. Is there any way to make a function that follows the first URL and returns the true location of the video file (the second URL)?

    Thanks in advance for any help.
     
    jakejarvis, Apr 22, 2007 IP
  2. Jim_

    Jim_ Peon

    Messages:
    72
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    function getRedirect($url)
    {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      $result = curl_exec($ch);
      curl_close($ch);
      preg_match("/Location: (.*?)\n/", $result, $match);
      $realurl = $match[1];
      return $realurl;
    }
    PHP:
    That should work.
     
    Jim_, Apr 22, 2007 IP
  3. jakejarvis

    jakejarvis Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Worked like a charm, thanks!
     
    jakejarvis, Apr 22, 2007 IP