0
我想讓一個API獲取谷歌驅動器視頻鏈接播放JW播放器和videojs爲mysite像(api.getlinkdrive.com),但我只是找不到方法來做到這一點。
Getlink谷歌驅動器IPV4
最近,我在github上發現了一些源代碼(https://github.com/marxvn/gdrive)。我爲我的網站修改了它。代碼工作正常,但鏈接無法播放(請參閱示例:http://getlinkdrive.byethost7.com/examples/jwplayer.php)。
問題可能來自IP或文件的可訪問性。我只想知道我是否需要處理IP或類似OAuth 2.0以訪問Google API。
這裏是我的代碼: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; } }
這裏是玩家的代碼: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>
當我直接鏈接複製到瀏覽器中它顯示
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
該文件實際上已經公開。你給的方法只適用於html5播放器,但是我使用jw player和videojs的方法。 –
我想我可能需要使用代理,但現在我對此一無所知。 –