2014-02-12 2 views
1

Google 드라이브 파일 업로드 코드를 사용하고 있습니다. 다음 코드는 브라우저에서 올바르게 실행됩니다. 하지만 우리가 그것을하려고하면 우리는 오류 : fgets() 매개 변수 1 리소스, 주어진 문자열이 주어진다 나는 많은 해결책을 시도했지만 그것의 작동하지 않습니다. 웹 서비스에이 코드를 사용하여 파일을 Google 드라이브에 업로드하는 방법을 알려주세요.Google 드라이브에 파일 업로드 (PHP 웹 서비스 사용)

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



function uploadFile() 
{ 

$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')); 


if(!defined("STDIN")) define("STDIN", "fopen('php://stdin','r')"); 

$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)); 
//$authCode = trim(file_get_contents(STDIN)); 



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

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

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

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

print_r($createdFile); 

} 

감사

답변

0

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 파일 token.json에서 인증 정보를 얻으십시오. 다음을 사용하여 업로드 할 수 있습니다. ...

$service = new Google_DriveService($client); 

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

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

json을 얻으면 상위 코드를 다시 실행하지 마십시오 ... 매번 token.json을 사용하여 업로드/다운로드하십시오.

관련 문제