2017-04-18 3 views
0

Ich möchte eine API bekommen Google Drive Video Link auf JW Player und videojs für mysite wie (api.getlinkdrive.com) spielen, aber ich konnte einfach nicht die Methode finden, es zu tun.
Getlink Google Drive IPV4

Kürzlich habe ich einige Quellcode auf GitHub (https://github.com/marxvn/gdrive) gefunden. Ich habe es für meine Website geändert. Der Code funktioniert einfach, aber der Link kann nicht abgespielt werden (siehe Beispiel hier: http://getlinkdrive.byethost7.com/examples/jwplayer.php).

Das Problem kann von der IP oder vielleicht der Zugänglichkeit der Datei kommen. Ich möchte nur wissen, ob ich mit IP oder etwas wie OAuth 2.0 umgehen muss, um auf Google API zuzugreifen oder nicht.

Hier ist mein Code: gdrive.php

 

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; 
    } 
} 

Hier ist der Code für den Spieler: jwplayer.php

<?php 
require __DIR__ . 'gdrive.php'; 
use \Marxvn\gdrive; 

$gdrive = new gdrive; 
$gdrive->getLink('https://drive.google.com/file/d/0B4EeKbDRC_36QzVNd2xnUEJfU28/view'); 

?> 

<!DOCTYPE html> 
<html lang="vi"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <title>Jwplayer</title> 
</head> 
<body> 
    <div id='player'></div> 

<script type="text/javascript" src="http://djrocker-anime.com/jwplayer/jwplayer.js"></script> 
<script src="https://content.jwplatform.com/libraries/cJ0z4Ufh.js"></script> 
<script type='text/javascript'> 
    jwplayer.key='r1br2DJmUxLwrLgpi7D4IjgUHoHsDvGWHw2T7Q=='; 
    var playerInstance = jwplayer(player); 
    playerInstance.setup({ 
     sources: <?php echo $gdrive->getSources('jwplayer');?>, 
     width: '50%', 
     height: '50%', 
     aspectratio: '16:9', 
     fullscreen: 'true', 
     autostart: 'true', 
    }); 
</script> 
</body> 
</html> 

<pre> 
    <?php print_r($gdrive->getSources('jwplayer')); ?> 
</pre> 

Wenn ich den direkten Link in den Browser kopieren zeigt

 
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 

Antwort

0

Error 403 bedeutet, dass Sie nicht p Zugriff auf die Datei, die Sie anrufen. Was Sie tun können, ist die export request from Drive API mit einer Demo verwenden, um von diesem SO thread

Verwenden Sie diesen Link im Browser oder Download zu streamen:

https://drive.google.com/uc?export=download&id=ID_OF_YOUR_DRIVE_VIDEO 

Bis zu Ihnen in PHP zu implementieren.

+0

Die Datei ist tatsächlich bereits öffentlich freigegeben. Die Methode, die Sie angegeben haben, funktioniert nur mit html5 player, aber die Methode, die ich verwende, funktioniert sowohl für jw player als auch für videojs. –

+0

Ich denke, ich muss vielleicht mit Proxy arbeiten, aber jetzt weiß ich nichts darüber. –