title: Getlink Google Drive IPV4
body: <p>I want to make an API get google drive video link to play on JW player and videojs for mysite like (api.getlinkdrive.com), but I just could not find the method to do it.
  <br></p>
  
  <p>Recently, I have found some source code on github (<a href="https://github.com/marxvn/gdrive" rel="nofollow noreferrer">https://github.com/marxvn/gdrive</a>). I've modified it for my site. The code just work fine, but the link can't play (see example here: <a href="http://getlinkdrive.byethost7.com/examples/jwplayer.php" rel="nofollow noreferrer">http://getlinkdrive.byethost7.com/examples/jwplayer.php</a>).
  <br></p>
  
  <p>The problem may come from the IP or maybe accessibility of the file. I just want to know if I will have to deal with IP or something like OAuth 2.0 to access to google API or not.<br></p>
  
  <p>Here is my code: gdrive.php<br></p>
  
  <pre>
  
  namespace Marxvn;
  /**
   * Google Drive library
   *
   * @author Marxvn
   * @copyright (c) 2016, Marxvn
   * @license https://spdx.org/licenses/BSD-3-Clause.html BSD-3-Clause
   */
  
  class gdrive
  {
      /**
       *
       * @var url
       */
      protected $url;
  
      /**
       *
       * @var title
       */
      protected $title = '';
  
      /**
       *
       * @var sources
       */
      public $sources;
  
      /**
       * 
       * @var itag
       */
      protected $itag = [
          37,
          22,
          59,
          18
      ];
  
  
      /**
       *
       * @var vidcode
       */
      protected $vidcode = [
          //2D Non-DASH
          '18'    => '360',
          '59'    => '480',
          '22'    => '720',
          '37'    => '1080',
          //3D Non-DASH
          '82'    => '360',
          '83'    => '240',
          '84'    => '720',
          '85'    => '1080'
      ];
  
      /**
       *
       * @param  array    $itags
       * @return void
       */
  
      public function setItag(array $itag)
      {
          $this->itag = $itag;
      }
  
      /**
       *
       * @param  array    $vidcode
       * @return void
       */
  
      public function setVidcode(array $vidcode)
      {
          $this->vidcode = $vidcode + $this->vidcode;
      }
  
      /**
       *
       * @param  array    $title
       * @return void
       */
  
      public function setTitle($title)
      {
          $this->title = $title;
      }
  
      /**
       *
       * @param  string   $gurl
       * @return array
       */
  
      public function getLink($gurl)
      {
          $source = [];
  
          if( $this->getDriveId($gurl) ) {
              $body = $this->getByfopen();
  
              if($body && $this->getStr($body,'status=', '&') === 'ok') {
  
                  $fmt = $this->getStr($body, 'fmt_stream_map=','&');
  
                  $urls = explode(',', urldecode($fmt));
  
                  foreach ($urls as $url) {
                      list($itag,$link) = explode('|', $url);
                      if(in_array($itag, $this->itag)){
                          $source[$this->vidcode[$itag]] = preg_replace("/[^\/]+\.google\.com/", "redirector.googlevideo.com",$link);
                      }
                  }
              }
          }
          $this->sources = $source;
  
      }
  
      /**
       *
       * @param  string   $type
       * @return json
       */
  
      public function getSources($type = 'jwplayer')
      {
          $s = [];
  
          $url_tag = ($type == 'jwplayer') ? 'src' : 'file';
  
          foreach ($this->sources as $itag => $link) {
              $s[] = [
                  'type'  => 'video/mp4',
                  'label' => $itag,
                  'file'  => $link.'&tile='.$itag,
                  $url_tag => $link.'&tile='.$this->title.'-'.$itag
              ];
          }
          return json_encode($s);
      }
  
      /**
       *
       * @param  string   $url
       * @return string
       */
      public function getByfopen()
      {
          try {
              $handle = fopen($this->url, "r");
  
              if ( !$handle ) {
                  throw new \Exception('Url open failed.');
              }  
  
              $contents = stream_get_contents($handle);
              fclose($handle);
  
              return $contents ? $contents : '';
  
          } catch( \Exception $e) {
              echo 'Message: ' .$e->getMessage();
          }
      }
  
      /**
       *
       * @param  string   $url
       * @return mixed
       */
  
      private function getDriveId($url)
      {
          preg_match('/(?:https?:\/\/)?(?:[\w\-]+\.)*(?:drive|docs)\.google\.com\/(?:(?:folderview|open|uc)\?(?:[\w\-\%]+=[\w\-\%]*&)*id=|(?:folder|file|document|presentation)\/d\/|spreadsheet\/ccc\?(?:[\w\-\%]+=[\w\-\%]*&)*key=)([\w\-]{28,})/i', $url , $match);
  
          if(isset($match[1])) {
              $this->url = 'https://docs.google.com/get_video_info?docid='.$match[1];
              return true;
          }
  
          return false;
      }
  
      /**
       *
       * @param  string   $string
       * @param  string   $find_start
       * @param  string   $find_end
       * @return mixed
       */
      private function getStr($string, $find_start, $find_end)
      {
          $start = stripos($string, $find_start);
  
          if($start === false) return false;
  
          $length = strlen($find_start);
  
          $end = stripos(substr($string, $start+$length), $find_end);
  
          if($end !== false) {
              $rs = substr($string, $start+$length, $end);
          } else {
              $rs = substr($string, $start+$length);
          }
  
          return $rs ? $rs : false;
      }
  }
  </pre>
  
  <p>Here is the code for the player: jwplayer.php</p>
  
  <pre><code>&lt;?php
  require __DIR__ . 'gdrive.php';
  use \Marxvn\gdrive;
  
  $gdrive = new gdrive;
  $gdrive-&gt;getLink('https://drive.google.com/file/d/0B4EeKbDRC_36QzVNd2xnUEJfU28/view');
  
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang="vi"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;title&gt;Jwplayer&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id='player'&gt;&lt;/div&gt;
  
  &lt;script type="text/javascript" src="http://djrocker-anime.com/jwplayer/jwplayer.js"&gt;&lt;/script&gt;
  &lt;script src="https://content.jwplatform.com/libraries/cJ0z4Ufh.js"&gt;&lt;/script&gt;
  &lt;script type='text/javascript'&gt;
      jwplayer.key='r1br2DJmUxLwrLgpi7D4IjgUHoHsDvGWHw2T7Q==';
      var playerInstance = jwplayer(player);
      playerInstance.setup({
          sources: &lt;?php echo $gdrive-&gt;getSources('jwplayer');?&gt;,
          width: '50%',
          height: '50%',
          aspectratio: '16:9',
          fullscreen: 'true', 
          autostart: 'true',  
      });
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;pre&gt;
      &lt;?php print_r($gdrive-&gt;getSources('jwplayer')); ?&gt;
  &lt;/pre&gt;
  </code></pre>
  
  <p>When I copy the direct link to the browser it shows</p>
  
  <pre>
  403. That’s an error.
  
  Your client does not have permission to get URL /videoplayback?id=2f1b23544d05d291&itag=18&source=webdrive&requiressl=yes&ttl=transient&mm=30&mn=sn-aigllner&ms=nxu&mv=u&pl=22&ei=fSn2WMKwOpTmqgWLsS8&mime=video\/mp4&lmt=1428053688993343&mt=1492527374&ip=185.27.134.64&ipbits=0&expire=1492541885&cp=QVJOVEZfUFhWRlhNOnNmWVVyQ3NPR2Fl&sparams=ip%2Cipbits%2Cexpire%2Cid%2Citag%2Csource%2Crequiressl%2Cttl%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cei%2Cmime%2Clmt%2Ccp&signature=B682DF0E2471F5AB76BF14B2E4CF0C48CDF247BA.62EFECACDDA2D0B2374CC885C2F5804A5976E340&key=ck2&app=explorer&driveid=0B4EeKbDRC_36QzVNd2xnUEJfU28 from this server
  </pre>