PHP - Google Drive API HTTP响应

时间:2016-09-06 03:50:27

标签: php google-api google-drive-api

我正在制作一些脚本,用于在google驱动器中下载我的文件。

这里是我的test.php

require_once 'google-api-php-client/Google_Client.php';
require_once 'google-api-php-client/contrib/Google_DriveService.php';
$drive = new Google_Client();
$drive->setClientId('709390325823-6bljk00f39xxxxx6jg3ji.apps.googleusercontent.com');
$drive->setClientSecret('UL07mxxxxx01eA'); 
$drive->setRedirectUri('http://xxx');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));
$drive->setAccessType('offline');
$gdrive = new Google_DriveService($drive);
$url = $drive->createAuthUrl();
 $token = $drive->authenticate($authorizationCode);
$fileId = "0B9LPSEw89uQNQVNuUzdwM2hFMlk";
$file = $gdrive->files->get($fileId);
      $downloadUrl = $file->downloadUrl;

 if ($downloadUrl) {
 $request = new Google_Http_Request($downloadUrl, 'GET', null, null);
$io = $drive->getIo();
 $httpRequest = $io->makeRequest($request);
 if ($httpRequest->getResponseHttpCode() == 200) {
 return $httpRequest->getResponseBody();
} else {

 error_log("Error Downloading Attachment - HTTP ResponseCode:".$httpRequest->getResponseHttpCode());
error_log("ContentUrl:         ".$attachment->getContentUrl());
error_log("ContentUrl:         ".$attachment->getContentUrl());
error_log("ContentType:        ".$attachment->getContentType());
error_log("id                  ".$attachment->getId());
error_log("IsProcessing:       ".(int)$attachment->getIsProcessingContent());
  return null;
}

 } else {
// An error occurred.
}

当我运行downloadUrl时,一切看起来都很好,它显示了downloadUrl和token

  

https://doc-0g-1s-docs.googleusercontent.com/docs/securesc/qjrgdcogjm0ajgdp4fnjceh8pfg486sg/u39dld0fisj9v0b3ih1nmchqp4495gdl/1473127200000/02390580194345273532/02390580194345273532/0B9LPSEw89uQNQVNuUzdwM2hFMlk?e=download&gd=true&access_token=ya29.Ci9WAxf99iwravP2CMmXEaYJWZb-1Y2Dxze1HToAyLb0_JJGrdVjFA5goQVBVRmWKg

但显示错误

  

致命错误:在

中的非对象上调用成员函数getResponseHttpCode()

api google v2中的http响应功能是什么?调用函数或缺少函数我错了吗?

希望你们给出一些建议。感谢

1 个答案:

答案 0 :(得分:0)

我建议您查看有关PHP client library实施方式的示例代码。

以下是下载Google文档(文档,电子表格,演示文稿等)的文档中的示例代码,并将其导出为您的应用可以处理的格式。

Google Drive v2

/**
* Download a file's content.
*
* @param Google_Servie_Drive $service Drive API service instance.
* @param Google_Servie_Drive_DriveFile $file Drive File instance.
* @return String The file's content if successful, null otherwise.
*/
function downloadFile($service, $file) {
$downloadUrl = $file->getDownloadUrl();
if ($downloadUrl) {
$request = new Google_Http_Request($downloadUrl, 'GET', null, null);
$httpRequest = $service->getClient()>getAuth()>authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
// An error occurred.
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}

以下是文档中的示例代码。

Google Drive v3

$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$content = $driveService->files->get($fileId, array(
'alt' => 'media' ));

希望它有所帮助!