Hello everyone,
I was working on a project where i need to display a video specifically from my google drive on a webpage. I don't want to manually select the video and copy its embed code, is there a way we can obtain the embed code for a video using google api (PHP preferred) ?
So that i can use this code within an iframe and display the video.
Please also suggest if there are other ways.
Thanks in advance,
You can start with Google Drive API https://developers.google.com/drive/v3/web/quickstart/php and this segment of code i hope can help you.
<?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>'
}
?>
[Info]
The objects returned from $service->files->listFiles($optParams)
are Google_Service_Drive_DriveFile objects and each of these have a series of properties. From v3 of Google API a properties called embedLink
was removed and there isn't an alternative for this property from v2(Migrate to Google Drive API v3) so as you seen in my code i use webViewLink property that return a URL to a view of file like this:
https://drive.google.com/file/d/123456789/view
But if you use this URL inside an <iframe>
the browser notice to you an error:
Refused to display 'https://drive.google.com/file/d/123456789/view' in a frame because it set 'X-Frame-Options' to 'sameorigin'.`
I'm not holding you too much on this issue and there are a lot of question about this. So we need to request a preview of file and not a view an i do that with
str_replace('/view', '/preview', $file->webViewLink);
on the URL returned by the request. Now you can use this URL in a <iframe>