簡體   English   中英

你能告訴我這個用 PHP 編寫的谷歌驅動 API 調用函數有什么問題嗎?

[英]can you tell me what's wrong with this google drive API call function in PHP

我有這段代碼可以運行並從我的驅動器中獲取圖像。 但是每次運行此代碼時都會遇到問題。

  function listF() {

    $result = array();
    $tok = array();
    $nextPageToken = NULL;
  do {
    try {
      $parameters = array();
      if ($nextPageToken) {
        $parameters['pageToken'] = $nextPageToken;
        $parameters['q'] = "mimeType='image/jpeg' or mimeType='image/png'";
      }
      $files = $this->service->files->listFiles($parameters);
      $tok[] = $nextPageToken;
      $result = array_merge($tok, $result, $files->getFiles());
      $nextPageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $nextPageToken = NULL;
    }
  } while ($nextPageToken);
  return $result;
}

我收到此錯誤:

An error occurred: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid Value",
    "locationType": "parameter",
    "location": "pageToken"
   }
  ],
  "code": 400,
  "message": "Invalid Value"
 }
}

對我來說,這似乎並不違法。 也許你能找到這個錯誤。 謝謝

我將使用 Javascript 回答您的nextPageToken問題,只需注意邏輯即可。 我有兩個相同的 listFile() 函數。 一個在初始加載時執行,加載頁面后,它顯示我的 100 個文件中的前 10 個。 每次單擊按鈕時都會執行另一個。

顯示初始 10 個文件的第一個函數

//take note of this variable
  var nextToken ;
  function listFiles() {
    gapi.client.drive.files.list({
      'pageSize': 10,
      'fields': "*"
    }).then(function(response) {

          //assign the nextPageToken to a variable to be used later
          nextToken = response.result.nextPageToken;
          // do whatever you like here, like display first 10 files in web page
          // . . .
        });
      }

第二個功能:這個功能是通過點擊一個名為“下一頁”的按鈕觸發的,該按鈕顯示從 11 到 N 的后續文件。

 function gotoNextPage(event) {
          gapi.client.drive.files.list({
            'pageSize': 10,
            'fields': "*",
            'pageToken': nextToken
          }).then(function(response) {
            //assign new nextPageToken to make sure new files are displayed
            nextToken = response.result.nextPageToken;
            //display batch of file results in the web page
            //. . .          
          });
  }

除非您在初始請求中包含的后續請求中包含完全相同的查詢字段 (q),否則 nextPageToken 似乎將被裁定為無效。

var files = []
var nextToken;
gapi.client.drive.files.list({
    'q': "mimeType='image/jpeg' or mimeType='image/png'",   
    'pageSize': 10,
    'fields': 'nextPageToken, files(id, name)'
}).then(function(response) {
    nextToken = response.result.nextPageToken;
    files.push(...response.result.files)
    while (nextToken) {
        gapi.client.drive.files.list({
            'nextPage': nextToken,
            'q': "mimeType='image/jpeg' or mimeType='image/png'",   
            'pageSize': 10,
            'fields': 'nextPageToken, files(id, name)'
        }).then(function(response) {
            nextToken = response.result.nextPageToken;
            files.push(...response.result.files)
        })
    }
});

Google Drive V3 PHP API 的文檔不像 V2 那樣豐富。

我沒有發現將pageToken用於 V3 API 的簡單 PHP 示例,因此我提供了以下示例:

 $parameters = array();
 $parameters['q'] = "mimeType='image/jpeg' or mimeType='image/png'";
 $parameters['fields'] = "files(id,name), nextPageToken";
 $parameters['pageSize'] = 100;
 $files = $google_drive_service->files->listFiles($parameters);

 /* initially, we're not passing a pageToken, but we need a placeholder value */
 $pageToken = 'go';

 while ($pageToken != null) {
     if (count($files->getFiles()) == 0) {
        echo "No files found.\n";
     } 
     else {
             foreach ($files->getFiles() as $file) {
                echo "name: '".$file->getName()."' ID: '".$file->getId()."'\n";
             }
     }

    /* important step number one - get the next page token (if any) */
    $pageToken = $files->getNextPageToken(); 

    /* important step number two - append the next page token to your query */
    $parameters['pageToken'] = $pageToken;
 
    $files = $google_drive_service->files->listFiles($parameters);
}

經過 V3 測試的解決方案。 這將通過處理分頁來處理大集合(大於 1000):

function GetFiles()
{

    $options =
    [
        'pageSize' => 1000,
        'supportsAllDrives' => true,
        'fields' => "files(id, mimeType, name), nextPageToken"
    ];

    $files = [];
    $pageToken = null;

    do
    {
        try
        {
            if ($pageToken !== null)
            {
                $options['pageToken'] = $pageToken;
            }

            $response = $this->service->files->listFiles($options);

            $files = array_merge($files, $response->files);
            $pageToken = $response->getNextPageToken();
        }
        catch (Exception $exception)
        {
            $message = $exception->getMessage();
            echo "Error: $message\r\n";
            $pageToken = null;
        }
    } while ($pageToken !== null);

    return $files;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM