小编mir*_*oft的帖子

Google Drive PHP API - 简单文件上传

我正在尝试编写一个小脚本,使用Google Drive PHP API将本地文件上传到Google云端硬盘.文档维护得很差,但到目前为止,我很确定代码应该是这样的:

<?php

include_once 'Google/Client.php';
include_once 'Google/Service/Drive.php';
include_once 'Google/Auth/OAuth2.php';

$client = new Google_Client();

$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setClientId('dfgdfgdg');
$client->setClientSecret('dfgdfgdf');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');


$service = new Google_Service_Drive($client);

$data = file_get_contents("a.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',
    'uploadType' => 'media',
));
}
catch (Exception $e)
{
    print $e->getMessage();
}

print_r($createdFile);

?>
Run Code Online (Sandbox Code Playgroud)

问题是我无法正确进行身份验证(或者我正在做其他错误的事情?).我收到的错误是:

HTTP Error: Unable to connect: 'fopen(compress.zlib://https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart) …
Run Code Online (Sandbox Code Playgroud)

php google-drive-api google-api-php-client

18
推荐指数
1
解决办法
6万
查看次数