【问题标题】:How to use pagination to retrieve next result set?如何使用分页检索下一个结果集?
【发布时间】:2016-06-16 14:12:53
【问题描述】:

我有 Google Analytics PHP API。一切正常。但我需要得到 [nextLink] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:.... 来自[nextLink] 的数据。
所以问题是如何通过在结果数组中使用[nextLink] url 来获取下一页数据

我知道有人问过once 几乎类似的问题,但它没有答案,所以我问它因为我找不到解决方案。

我非常有兴趣看到一些小的 php 代码示例,请加分,谢谢:)

结果我看到了数据数组:

[columnHeadersType:protected] => Google_Service_Analytics_GaDataColumnHeaders
    [columnHeadersDataType:protected] => array
    [containsSampledData] => 
    [dataTableType:protected] => Google_Service_Analytics_GaDataDataTable
    [dataTableDataType:protected] => 
    [id] => https://www.googleapis.com/analytics..........
    [itemsPerPage] => 1000
    [kind] => analytics#gaData
    [nextLink] => https://www.googleapis.com/analytics/v3/data........&start-index=1001&max-results=1000
    [previousLink] => 
    [profileInfoType:protected] => Google_Service_Analytics_GaDataProfileInfo
    [profileInfoDataType:protected] => 
    [queryType:protected] => Google_Service_Analytics_GaDataQuery
    [queryDataType:protected] => 
    [rows] => Array

主要的php代码是:

function getService()
{
  // Creates and returns the Analytics service object.

  // Load the Google API PHP Client Library.
require_once LOCATION.'/google/vendor/autoload.php';


  // Use the developers console and replace the values with your
  // service account email, and relative location of your key file.
  $service_account_email = 'xxxx-xxxxx@xxxxxxxxx.gserviceaccount.com';
  $key_file_location = LOCATION.'/xxxx.p12';

  // Create and configure a new client object.
  $client = new Google_Client();
  $client->setApplicationName("HelloAnalytics");
  $analytics = new Google_Service_Analytics($client);

  // Read the generated client_secrets.p12 key.
  $key = file_get_contents($key_file_location);
  $cred = new Google_Auth_AssertionCredentials(
      $service_account_email,
      array(Google_Service_Analytics::ANALYTICS_READONLY),
      $key
  );
  $client->setAssertionCredentials($cred);
  if($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
  }

  return $analytics;
}

function getFirstprofileId(&$analytics) {
  // Get the user's first view (profile) ID.

  // Get the list of accounts for the authorized user.
  $accounts = $analytics->management_accounts->listManagementAccounts();

  if (count($accounts->getItems()) > 0) {
    $items = $accounts->getItems();
    $firstAccountId = $items[0]->getId();

    // Get the list of properties for the authorized user.
    $properties = $analytics->management_webproperties
        ->listManagementWebproperties($firstAccountId);

    if (count($properties->getItems()) > 0) {
      $items = $properties->getItems();
      $firstPropertyId = $items[0]->getId();

      // Get the list of views (profiles) for the authorized user.
      $profiles = $analytics->management_profiles
          ->listManagementProfiles($firstAccountId, $firstPropertyId);

      if (count($profiles->getItems()) > 0) {
        $items = $profiles->getItems();

        // Return the first view (profile) ID.
        return $items[0]->getId();

      } else {
        throw new Exception('No views (profiles) found for this user.');
      }
    } else {
      throw new Exception('No properties found for this user.');
    }
  } else {
    throw new Exception('No accounts found for this user.');
  }
}

function getResults(&$analytics, $profileId) {

      $params = array('dimensions' => 'ga:pagePath,ga:deviceCategory,ga:city,ga:operatingSystem,ga:screenResolution,ga:sourceMedium,ga:keyword');
     return $analytics->data_ga->get(
       'ga:' . $profileId,
       'yesterday',
       'yesterday',
       'ga:sessions,ga:hits,ga:sessionDuration',$params);
}

function printResults(&$results) {
  // Parses the response from the Core Reporting API and prints
  // the profile name and total sessions.
  if (count($results->getRows()) > 0) {

    // Get the profile name.
    $profileName = $results->getProfileInfo()->getProfileName();

    // Get the entry for the first entry in the first row.
    $rows = $results->getRows();


    echo '<pre>';
    print_r($rows);


  } else {
    print "No results found.\n";
  }
}

$analytics = getService();
$profile = getFirstProfileId($analytics);
$results = getResults($analytics, $profile);
echo '<pre>';
print_r($results);
echo '<br>';

【问题讨论】:

    标签: php pagination google-api google-analytics-api google-api-php-client


    【解决方案1】:

    应该是这样的。 Pagination

    $token = $results->getNextPageToken();
    while($token != null) {
    
    
        $params = array('dimensions' => 'ga:pagePath,ga:deviceCategory,ga:city,ga:operatingSystem,ga:screenResolution        ,ga:sourceMedium,ga:keyword',
                    'pageToken' => $token);
    
        $result = $analytics->data_ga->get('ga:' . $profileId,
                                       'yesterday',
                                       'yesterday',
                                       'ga:sessions,ga:hits,ga:sessionDuration',                                       $params);
       $token = $results->getNextPageToken();
    
    }
    

    对不起,我现在没有 php 的能力,所以我无法测试它。但它应该很接近。你只需要检查第一个请求中是否有下一个链接,如果它们是我通常只是循环它直到我拥有一切。

    提示:请记住,每次获取下一个链接时,您都会将 1 计入您的配额。

    【讨论】:

    • 非常感谢。我要去看看这个:D
    猜你喜欢
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2010-11-13
    • 2013-01-30
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 2019-10-05
    相关资源
    最近更新 更多