2

1 - Google ピッカーを構成しましたが、正常に動作しており、ピッカーからファイルを選択してファイル 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 関数を使用して、Google ドライブからファイルを取得し、サーバーに保存します。サーバー ディレクトリにファイルが作成されましたが、切り取られました。私はこのコードを使用します

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

サーバーに破損したファイルが保存されますが、このコードを使用してGoogleドライブ以外のファイルを取得すると、サーバーに正常にダウンロードされます。

php を使用して $downloadUrl からサーバーにファイルをダウンロードする方法を教えてください。

4

0 に答える 0