2013-11-25 51 views
2

我試圖將文件提交到Google Drive。雖然文件已上傳到驅動器,但遇到接受按鈕後出現錯誤。將文件提交給Google驅動器

<?php 
require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_DriveService.php'; 

$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId('YOUR_CLIENT_ID'); 
$client->setClientSecret('YOUR_CLIENT_SECRET'); 
$client->setRedirectUri('http://localhost/pdf/quickstart.php'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive')); 

$service = new Google_DriveService($client); 

$authUrl = $client->createAuthUrl(); 

//Request authorization 
print "Please visit:\n$authUrl\n\n"; 
print "Please enter the auth code:\n"; 
$authCode = trim(fgets(STDIN)); 

// Exchange authorization code for access token 
$accessToken = $client->authenticate($authCode); 
$client->setAccessToken($accessToken); 

//Insert a file 
$file = new Google_DriveFile(); 
$file->setTitle('My document'); 
$file->setDescription('A test document'); 
$file->setMimeType('text/plain'); 

$data = file_get_contents('document.txt'); 

$createdFile = $service->files->insert($file, array(
     'data' => $data, 
     'mimeType' => 'text/plain', 
    )); 

print_r($createdFile); 
?> 

錯誤消息:

說明:使用未定義的常量STDIN的 - 假定 'STDIN' 在C:\的LocalServer \ htdocs中\ PDF \ quickstart.php上線18

警告:與fgets()預計參數1是資源,在串C中給出:\的LocalServer \ htdocs中\ PDF \上線quickstart.php 18

如何可以固定

+0

那麼,什麼是'STDIN'和它來自哪裏?我看不到這條線上的任何地方 –

+0

這裏仍然//請求授權 打印「請訪問:\ n $ authUrl \ n \ n」; 打印「請輸入驗證碼:\ n」; $ authCode = trim(fgets(STDIN)); –

+0

我看到了。你的問題是你正在使用一個名爲'STDIN'的常量,它在任何地方都沒有定義。它應該是一個變量,例如'$ STDIN'?沒有看到定義的常量/變量的位置,我們無法幫助您。 –

回答

2

STDIN是一個命令行指令,它不是一個直接有效的PHP語句.. 我所做的是:

$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId('**********************'); 
$client->setClientSecret('*********'); 
$client->setRedirectUri('*********'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive')); 

$authUrl = $client->createAuthUrl(); 
print $authurl 

$authCode = ''; // insert the verification code that you get after going to url in $authurl 
file_put_contents('token.json', $client->authenticate($authCode)); 

執行這麼多,你會得到在JSON文件令牌身份驗證信息後,以.json ...你可以使用下面的上傳...:

$service = new Google_DriveService($client); 

$client->setAccessToken(file_get_contents('token.json')); 

//Insert a file 
    ..... // rest the same 

一旦你的JSON不要再次運行前的代碼...只需使用token.json每次上傳/下載....

相關問題