Google Drive PHP API - Simple File Upload(Google Drive PHP API - 简单文件上传)
问题描述
我正在尝试使用 Google Drive PHP API 编写一个小脚本来将本地文件上传到 Google Drive.文档维护得很差,但到目前为止我很确定代码应该是这样的:
I am trying to write a small script to upload a local file to Google Drive, using Google Drive PHP API. The documentation is very poor maintained, but so far I am pretty sure the code should be looking like that:
<?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);
?>
问题是我无法正确进行身份验证(或者我做错了什么?).我收到的错误是:
The issue is I am not able to do the authentication right (or I am doing something else wrong?). The error I received is:
HTTP Error: Unable to connect: 'fopen(compress.zlib://https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart) [function.fopen]: failed to open stream: operation failed'
随后出现此错误:
Notice: Undefined variable: createdFile in C:wampwwwGoogleAPIindex.php on line 39
我做错了什么?您能否提供一个使用 Google Drive PHP API 将文件上传到 Google Drive 的简单工作脚本?提前谢谢!
What am I doing wrong? Can you provide a simple working script of uploading a file to Google Drive using Google Drive PHP API? Thank you in advance!
推荐答案
使用此代码验证并上传测试文件.您需要将 <YOUR_REGISTERED_REDIRECT_URI>
(以及在控制台中)设置到此文档本身以进行身份验证.
Use this code to authenticate and upload a test file. You need to set <YOUR_REGISTERED_REDIRECT_URI>
(and also in console) to this document itself to authenticate.
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('<YOUR_CLIENT_ID>');
$client->setClientSecret('<YOUR_CLIENT_SECRET>');
$client->setRedirectUri('<YOUR_REGISTERED_REDIRECT_URI>');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
session_start();
if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
} else
$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Drive($client);
//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setName(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');
$data = file_get_contents('a.jpg');
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'
));
print_r($createdFile);
} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit();
}
这篇关于Google Drive PHP API - 简单文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Google Drive PHP API - 简单文件上传
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01