我有一些代码可以执行以下操作:
$file_name.rtf
将rtf
文件从服务器上传并转换为用户的Google云端硬盘文件夹。
rtf
文档已完美创建,但$file_name
等变量已丢失,因此上传了空文件
谷歌驱动器。但是,$file_date variable
仍然存在。我想知道为什么如果用户尚未登录,此代码无效。我怀疑它与Google API客户端的这一部分有关:
if (isset($_REQUEST['logout'])) {
unset($_SESSION['upload_token ']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['upload_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['upload_token']);
}
}
但是我不确定在登录后需要改变什么才能使用。
简化代码如下:
<?php
session_start();
date_default_timezone_set('America/New_York');
$dir = dirname(__FILE__);
require_once $dir . '/../../lib/PHPRtfLite.php';
// register PHPRtfLite class loader
PHPRtfLite::registerAutoloader();
// Bunch of code that creates an RTF document
// Save rtf document
$file_date = date("n.j.y");
$file_name = $file_date . " - " . $invoice_number . " - " . $customer_name;
$rtf->save($dir . '/' . $file_name . '.rtf');
?>
// Begin Google API Client
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once "templates/base.php";
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Http/MediaFileUpload.php';
require_once 'Google/Service/Drive.php';
DEFINE("APPRAISAL", $file_name . '.rtf');
$client_id = 'xxxx';
$client_secret = 'xxxx';
$redirect_uri = 'xxxx';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['upload_token ']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['upload_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['upload_token']);
}
} else {
$authUrl = $client->createAuthUrl();
}
/************************************************
If we're signed in then lets try to upload our
file.
************************************************/
if ($client->getAccessToken()) {
$file = new Google_Service_Drive_DriveFile();
$file->title = $file_name;
$chunkSizeBytes = 1 * 1024 * 1024;
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->insert($file, array(
'convert' => true
));
// Create a media file upload to represent our upload process.
$media = new Google_Http_MediaFileUpload(
$client,
$request,
'application/rtf',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize(APPRAISAL));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen(APPRAISAL, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if ($status != false) {
$result = $status;
}
fclose($handle);
}
echo pageHeader("File Upload - Appraisal");
?>
<!doctype html>
<html>
<head>
<title>Congradulations!</title>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="box">
<div class="request">
<?php if (isset($authUrl)): ?>
<a class='login' href='<?php echo $authUrl; ?>'>Connect Me!</a>
<?php endif; ?>
</div>
<?php if (isset($result) && $result): ?>
<div>
<p>The link to the document is: <a href="<?php echo $result["alternateLink"]; ?>"><?php echo $file_name; ?></a></p>
</div>
<?php endif ?>
</div>
</body>
</html>
答案 0 :(得分:1)
此处有一个额外的空格' '
:
$_SESSION['upload_token '])
我很确定这会影响代码。
您基本上取消了从未设置过的会话,而不是取消设置$_SESSION['upload_token']
。
也许还有更多,但这应该是一个良好的开端。