title: php youtube video download
body: <p>I use below script to download youtube video, it works fine in xamp in localhost, but when i put it in my live wev server its not working , I get this code from <a href="https://github.com/jeckman/YouTube-Downloader/blob/master/getvideo.php">https://github.com/jeckman/YouTube-Downloader/blob/master/getvideo.php</a></p>
  
  <pre><code>  &lt;?php
      // YouTube Downloader PHP
      // based on youtube-dl in Python http://rg3.github.com/youtube-dl/
      // by Ricardo Garcia Gonzalez and others (details at url above)
      //
      // Takes a VideoID and outputs a list of formats in which the video can be
      // downloaded
  
      include_once('curl.php');
  
      if(isset($_REQUEST['videoid'])) {
          $my_id = $_REQUEST['videoid'];
      } else {
          echo '&lt;p&gt;No video id passed in&lt;/p&gt;';
          exit;
      }
  
      if(isset($_REQUEST['type'])) {
          $my_type =  $_REQUEST['type'];
      } else {
          $my_type = 'redirect';
      }
  
      if(isset($_REQUEST['debug'])) {
          $debug = TRUE;
      } else {
          $debug = FALSE;
      }
  
      if ($my_type == 'Download') {
      ?&gt;
  
      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
      &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
      &lt;head&gt;
          &lt;title&gt;Youtube Downloader&lt;/title&gt;
          &lt;meta name="keywords" content="Video downloader, download youtube, video download, youtube video, youtube downloader, download youtube FLV, download youtube MP4, download youtube 3GP, php video downloader" /&gt;
          &lt;link href="css/bootstrap.min.css" rel="stylesheet" media="screen"&gt;
           &lt;style type="text/css"&gt;
            body {
              padding-top: 40px;
              padding-bottom: 40px;
              background-color: #f5f5f5;
          }
  
            .download {
              max-width: 300px;
              padding: 19px 29px 29px;
              margin: 0 auto 20px;
              background-color: #fff;
              border: 1px solid #e5e5e5;
              -webkit-border-radius: 5px;
                 -moz-border-radius: 5px;
                      border-radius: 5px;
              -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                 -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                      box-shadow: 0 1px 2px rgba(0,0,0,.05);
            }
  
            .download .download-heading {
              margin-bottom: 10px;
            }
  
            .mime, .itag {
              width: 75px;
              display: inline-block;
            }
  
            .itag {
              width: 15px;
            }
  
            .userscript {
              float: right;
              margin-top: 5px
            }
          &lt;/style&gt;
          &lt;/head&gt;
      &lt;body&gt;
          &lt;div class="download"&gt;
          &lt;h1 class="download-heading"&gt;Youtube Downloader Results&lt;/h1&gt;
      &lt;?php
      } // end of if for type=Download
  
      /* First get the video info page for this video id */
      $my_video_info = 'http://www.youtube.com/get_video_info?&amp;video_id='. $my_id;
      $my_video_info = curlGet($my_video_info);
  
      /* TODO: Check return from curl for status code */
  
      parse_str($my_video_info);
      echo '&lt;p&gt;&lt;img src="'. $thumbnail_url .'" border="0" hspace="2" vspace="2"&gt;&lt;/p&gt;';
      $my_title = $title;
  
      if(isset($url_encoded_fmt_stream_map)) {
          /* Now get the url_encoded_fmt_stream_map, and explode on comma */
          $my_formats_array = explode(',',$url_encoded_fmt_stream_map);
          //if($debug) {
          //  echo '&lt;pre&gt;';
          //  print_r($my_formats_array);
          //  echo '&lt;/pre&gt;';
          //}
      } else {
          echo '&lt;p&gt;No encoded format stream found.&lt;/p&gt;';
          echo '&lt;p&gt;Here is what we got from YouTube:&lt;/p&gt;';
          echo $my_video_info;
      }
      if (count($my_formats_array) == 0) {
          echo '&lt;p&gt;No format stream map found - was the video id correct?&lt;/p&gt;';
          exit;
      }
  
      /* create an array of available download formats */
      $avail_formats[] = '';
      $i = 0;
  
      foreach($my_formats_array as $format) {
          parse_str($format);
          $avail_formats[$i]['itag'] = $itag;
          $avail_formats[$i]['quality'] = $quality;
          $type = explode(';',$type);
          $avail_formats[$i]['type'] = $type[0];
          $avail_formats[$i]['url'] = urldecode($url) . '&amp;signature=' . $sig;
          parse_str(urldecode($url));
          $avail_formats[$i]['expires'] = date("G:i:s T", $expire);
          $avail_formats[$i]['ipbits'] = $ipbits;
          $avail_formats[$i]['ip'] = $ip;
          $i++;
      }
  
      if ($debug) {
          echo '&lt;p&gt;These links will expire at '. $avail_formats[0]['expires'] .'&lt;/p&gt;';
          echo '&lt;p&gt;The server was at IP address '. $avail_formats[0]['ip'] .' which is an '. $avail_formats[0]['ipbits'] .' bit IP address. ';
          echo 'Note that when 8 bit IP addresses are used, the download links may fail.&lt;/p&gt;';
      }
      if ($my_type == 'Download') {
          echo '&lt;ul&gt;
                  List of available formats for download:&lt;br&gt;
                  &lt;small&gt;Right-click and choose "save as" or click "download" to use this server as proxy.&lt;/small&gt;
              &lt;/ul&gt;';
  
          /* now that we have the array, print the options */
          for ($i = 0; $i &lt; count($avail_formats); $i++) {
              echo '&lt;li&gt;' .
                      '&lt;span class="itag"&gt;' . $avail_formats[$i]['itag'] . '&lt;/span&gt; '.
                      '&lt;a href="' . $avail_formats[$i]['url'] . '" class="mime"&gt;' . $avail_formats[$i]['type'] . '&lt;/a&gt; ' .
                      '&lt;small&gt;(' .  $avail_formats[$i]['quality'] . ' / ' .
                      '&lt;a href="download.php?mime=' . $avail_formats[$i]['type'] .'&amp;title='. urlencode($my_title) .'&amp;token=' . base64_encode($avail_formats[$i]['url']) . '" class="dl"&gt;download&lt;/a&gt;' .
                      ')&lt;/small&gt;&lt;/li&gt;';
          }
          echo '&lt;/ul&gt;';
      ?&gt;
  
      &lt;!-- @TODO: Prepend the base URI --&gt;
      &lt;a href="ytdl.user.js" class="userscript btn btn-mini" title="Install chrome extension to view a 'Download' link to this application on Youtube video pages."&gt;
        Install Chrome Extension
      &lt;/a&gt;
  
      &lt;/body&gt;
      &lt;/html&gt;
  
      &lt;?php
  
      } else {
  
      /* In this else, the request didn't come from a form but from something else
       * like an RSS feed.
       * As a result, we just want to return the best format, which depends on what
       * the user provided in the url.
       * If they provided "format=best" we just use the largest.
       * If they provided "format=free" we provide the best non-flash version
       * If they provided "format=ipad" we pull the best MP4 version
       *
       * Thanks to the python based youtube-dl for info on the formats
       *                              http://rg3.github.com/youtube-dl/
       */
  
      $format =  $_REQUEST['format'];
      $target_formats = '';
      switch ($format) {
          case "best":
              /* largest formats first */
              $target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
              break;
          case "free":
              /* Here we include WebM but prefer it over FLV */
              $target_formats = array('38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13');
              break;
          case "ipad":
              /* here we leave out WebM video and FLV - looking for MP4 */
              $target_formats = array('37','22','18','17');
              break;
          default:
              /* If they passed in a number use it */
              if (is_numeric($format)) {
                  $target_formats[] = $format;
              } else {
                  $target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
              }
          break;
      }
  
      /* Now we need to find our best format in the list of available formats */
      $best_format = '';
      for ($i=0; $i &lt; count($target_formats); $i++) {
          for ($j=0; $j &lt; count ($avail_formats); $j++) {
              if($target_formats[$i] == $avail_formats[$j]['itag']) {
                  //echo '&lt;p&gt;Target format found, it is '. $avail_formats[$j]['itag'] .'&lt;/p&gt;';
                  $best_format = $j;
                  break 2;
              }
          }
      }
  
      //echo '&lt;p&gt;Out of loop, best_format is '. $best_format .'&lt;/p&gt;';
      $redirect_url = $avail_formats[$best_format]['url'];
      $content_type = $avail_formats[$best_format]['type'];
      header("Location: $redirect_url");
      } // end of else for type not being Download
      ?&gt;
  </code></pre>