【发布时间】:2018-05-10 14:02:41
【问题描述】:
大家好,
我正在做一个项目,我需要在网页上专门显示来自我的谷歌驱动器的视频。我不想手动选择视频并复制其嵌入代码,有没有一种方法可以使用 google api(首选 PHP)获取视频的嵌入代码?
这样我就可以在 iframe 中使用此代码并显示视频。
如果有其他方法,也请提出建议。
提前致谢,
【问题讨论】:
标签: php video iframe google-drive-api embed
大家好,
我正在做一个项目,我需要在网页上专门显示来自我的谷歌驱动器的视频。我不想手动选择视频并复制其嵌入代码,有没有一种方法可以使用 google api(首选 PHP)获取视频的嵌入代码?
这样我就可以在 iframe 中使用此代码并显示视频。
如果有其他方法,也请提出建议。
提前致谢,
【问题讨论】:
标签: php video iframe google-drive-api embed
您可以从 Google Drive API https://developers.google.com/drive/v3/web/quickstart/php 开始,我希望这段代码可以帮助到您。
<?php
// New Google Client, see all settings at link in description
$client = new Google_Client();
// New Google Drive Service
$service = new Google_Service_Drive($client);
// Params with filter of MIME type for search only videos mp4 (you can change this)
$optParams = [
'q' => "mimeType='video/mp4'",
'pageSize' => 10,
'fields' => 'nextPageToken, files(id, name, webViewLink)'
];
// Get files from our request
$files = $service->files->listFiles($optParams);
// Print an iframe for each video
foreach($files->files as $file){
// Now I need to make a little detail about the next lines of code so look at [Info]
$src = str_replace('/view', '/preview', $file->webViewLink);
echo '<iframe width="500" height="200" target="_parent" src="'. $src .'"></iframe>'
}
?>
[信息]
从$service->files->listFiles($optParams) 返回的对象是Google_Service_Drive_DriveFile 对象,每个对象都有一系列属性。从 Google API 的 v3 中删除了一个名为 embedLink 的属性,并且从 v2(Migrate to Google Drive API v3) 中没有替代此属性,因此正如您在我的代码中看到的,我使用 webViewLink 属性返回一个指向文件视图的 URL像这样:
https://drive.google.com/file/d/123456789/view
但如果您在 <iframe> 中使用此 URL,浏览器会通知您一个错误:
Refused to display 'https://drive.google.com/file/d/123456789/view' in a frame because it set 'X-Frame-Options' to 'sameorigin'.`
在这个问题上我并没有过多地阻止你,并且有一个 lot of question 关于这个。所以我们需要请求文件的预览,而不是我这样做的视图
str_replace('/view', '/preview', $file->webViewLink);
在请求返回的 URL 上。现在您可以在 <iframe> 中使用此 URL
【讨论】: