Отправка файла на диск Google

Я пытался отправить файл на Google Диск. Хотя файл загружен на диск, я получаю сообщение об ошибке после нажатия кнопки accept.

<?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

Как можно фиксировать

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 каждый раз, чтобы загружать / скачивать ….