Php 使用Google Drive SDK上载的文件不可见
我已经创建了一个简单的PHP脚本来使用Google的Drive SDK。我们的计划是,最终我们将使用GoogleDrive作为我们一些web内容的CDN形式(我们公司已经升级到1TB) 代码在一定程度上起作用,因为它成功地验证和上传了一个文件。问题是,文件总是被破坏,无法通过驱动器本身或下载来查看 代码相对简单,只需从Wikipedia获取图像并尝试上传:Php 使用Google Drive SDK上载的文件不可见,php,google-drive-api,Php,Google Drive Api,我已经创建了一个简单的PHP脚本来使用Google的Drive SDK。我们的计划是,最终我们将使用GoogleDrive作为我们一些web内容的CDN形式(我们公司已经升级到1TB) 代码在一定程度上起作用,因为它成功地验证和上传了一个文件。问题是,文件总是被破坏,无法通过驱动器本身或下载来查看 代码相对简单,只需从Wikipedia获取图像并尝试上传: <?php require_once 'Google/Client.php'; require_once 'Google/Servi
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
require_once 'Google/Service/Oauth2.php';
$client = new Google_Client();
//$client->setUseObjects(true);
//$client->setAuthClass('apiOAuth2');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setClientId('***');
$client->setClientSecret('***');
$client->setRedirectUri('***');
$client->setAccessToken(authenticate($client));
// initialise the Google Drive service
$service = new Google_Service_Drive($client);
$data = file_get_contents('http://upload.wikimedia.org/wikipedia/commons/3/38/JPEG_example_JPG_RIP_010.jpg');
// create and upload a new Google Drive file, including the data
try
{
//Insert a file
$file = new Google_Service_Drive_DriveFile($client);
$file->setTitle(uniqid().'.jpg');
$file->setMimeType('image/jpeg');
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
));
}
catch (Exception $e)
{
print $e->getMessage();
}
print_r($createdFile);
?>
执行print\r
语句,我们获得有关文件的信息。但是,正如我提到的,该文件不可查看,并且似乎已损坏。有人能解释一下问题是什么吗?您的代码似乎还可以。
你有没有试过从谷歌硬盘下载这个文件并查看一下?
另外,我知道这有点愚蠢,但在使用file\u get\u contents()之后,您是否尝试在磁盘上写入文件。你只需要确定它100%坏的点。在对文档做了更多的挖掘之后(当前的公共文档已经严重过时),我发现有必要发送另一个参数作为
insert()
函数的body
参数(函数调用中的第二个参数)的一部分
使用以下命令:
$createdFile = $service->files->insert($doc, array(
'data' => $content,
'mimeType' => 'image/jpeg',
'uploadType' => 'media', // this is the new info
));
我能让一切正常运转。我将把这个问题留在这里,因为我认为它将非常有用,直到Google实际更新PHP API的文档