2

1 - 我已经配置了谷歌选择器,它工作正常,我从选择器中选择文件并获取文件 ID。

2 - 在刷新令牌等所有过程之后,我获取文件元数据并获取文件导出链接

$downloadExpLink = $file->getExportLinks();
 $downloadUrl = $downloadExpLink['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];

3 - 之后我使用这个

if ($downloadUrl) {
     $request = new Google_HttpRequest($downloadUrl, 'GET', null, null);

        $httpRequest = Google_Client::$io->authenticatedRequest($request);
        if ($httpRequest->getResponseHttpCode() == 200) 
        {
            $content = $httpRequest->getResponseBody();
             print_r($content);


        } else {
        // An error occurred.
         return null;
     }

并得到这个回应

 [responseBody:protected] => PK��DdocProps/app.xml���
�0D���k�I[ѫ��m
��!����A={���}�
2G�Z�g�V��Bľ֧�n�Ҋ�ap!����fb�d����k}Ikc�_`t<+�(�NJ̽�����#��EU-
�0@���P����........

4 - 我使用一些 cURL 函数从谷歌驱动器获取文件并将其保存到服务器。在服务器目录中创建但已裁剪的文件。我使用此代码

$downloadExpLink = $file->getExportLinks();
$downloadUrl = $downloadExpLink['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];

//$downloadUrl value is
/*https://docs.google.com/feeds/download/documents/export/Export?id=1CEt1ya5kKLtgK************IJjDEY5BdfaGI&exportFormat=docx*/

当我将此 url 放入浏览器时,它将成功下载文件,但是当我使用此 url 使用 cURL 或任何 php 代码获取文件并尝试将其保存在服务器上时,它会保存损坏的文件。

$ch = curl_init();
$source = $downloadUrl;

curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$destination = "test/afile5.docx";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

它导致存储在服务器上的文件损坏,但是当我使用此代码获取除谷歌驱动器以外的任何文件时,我在服务器上成功下载了它。

任何人都可以帮助如何使用 php 将文件从 $downloadUrl 下载到我的服务器吗?

4

0 回答 0