title: How to use pagination to retrieve next result set?
body: <p><a href="https://i.stack.imgur.com/KeKDG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KeKDG.png" alt="enter image description here"></a></p>
  
  <p>I have Google Analytics PHP API. Everything works fine. But i need to get 
   <code>[nextLink] =&gt; https://www.googleapis.com/analytics/v3/data/ga?ids=ga:....</code> data from <code>[nextLink]</code>.<br>
  <strong>So question is how to get to the next page of data, by using <code>[nextLink]</code> url in result array</strong>.</p>
  
  <p>I know that almost similar question was asked <a href="https://stackoverflow.com/questions/28174492/google-analytics-api-how-to-retrieve-next-page-of-data">once</a> but it has no answers so i asked it cuz i can't find solution.</p>
  
  <p>I am very interested to see some little php code example, plus please, thanks :)</p>
  
  <h2>As result i see array of data:</h2>
  
  <pre><code>[columnHeadersType:protected] =&gt; Google_Service_Analytics_GaDataColumnHeaders
      [columnHeadersDataType:protected] =&gt; array
      [containsSampledData] =&gt; 
      [dataTableType:protected] =&gt; Google_Service_Analytics_GaDataDataTable
      [dataTableDataType:protected] =&gt; 
      [id] =&gt; https://www.googleapis.com/analytics..........
      [itemsPerPage] =&gt; 1000
      [kind] =&gt; analytics#gaData
      [nextLink] =&gt; https://www.googleapis.com/analytics/v3/data........&amp;start-index=1001&amp;max-results=1000
      [previousLink] =&gt; 
      [profileInfoType:protected] =&gt; Google_Service_Analytics_GaDataProfileInfo
      [profileInfoDataType:protected] =&gt; 
      [queryType:protected] =&gt; Google_Service_Analytics_GaDataQuery
      [queryDataType:protected] =&gt; 
      [rows] =&gt; Array
  </code></pre>
  
  <h1>Main php code is:</h1>
  
  <pre><code>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-&gt;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-&gt;setAssertionCredentials($cred);
    if($client-&gt;getAuth()-&gt;isAccessTokenExpired()) {
      $client-&gt;getAuth()-&gt;refreshTokenWithAssertion($cred);
    }
  
    return $analytics;
  }
  
  function getFirstprofileId(&amp;$analytics) {
    // Get the user's first view (profile) ID.
  
    // Get the list of accounts for the authorized user.
    $accounts = $analytics-&gt;management_accounts-&gt;listManagementAccounts();
  
    if (count($accounts-&gt;getItems()) &gt; 0) {
      $items = $accounts-&gt;getItems();
      $firstAccountId = $items[0]-&gt;getId();
  
      // Get the list of properties for the authorized user.
      $properties = $analytics-&gt;management_webproperties
          -&gt;listManagementWebproperties($firstAccountId);
  
      if (count($properties-&gt;getItems()) &gt; 0) {
        $items = $properties-&gt;getItems();
        $firstPropertyId = $items[0]-&gt;getId();
  
        // Get the list of views (profiles) for the authorized user.
        $profiles = $analytics-&gt;management_profiles
            -&gt;listManagementProfiles($firstAccountId, $firstPropertyId);
  
        if (count($profiles-&gt;getItems()) &gt; 0) {
          $items = $profiles-&gt;getItems();
  
          // Return the first view (profile) ID.
          return $items[0]-&gt;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(&amp;$analytics, $profileId) {
  
        $params = array('dimensions' =&gt; 'ga:pagePath,ga:deviceCategory,ga:city,ga:operatingSystem,ga:screenResolution,ga:sourceMedium,ga:keyword');
       return $analytics-&gt;data_ga-&gt;get(
         'ga:' . $profileId,
         'yesterday',
         'yesterday',
         'ga:sessions,ga:hits,ga:sessionDuration',$params);
  }
  
  function printResults(&amp;$results) {
    // Parses the response from the Core Reporting API and prints
    // the profile name and total sessions.
    if (count($results-&gt;getRows()) &gt; 0) {
  
      // Get the profile name.
      $profileName = $results-&gt;getProfileInfo()-&gt;getProfileName();
  
      // Get the entry for the first entry in the first row.
      $rows = $results-&gt;getRows();
  
  
      echo '&lt;pre&gt;';
      print_r($rows);
  
  
    } else {
      print "No results found.\n";
    }
  }
  
  $analytics = getService();
  $profile = getFirstProfileId($analytics);
  $results = getResults($analytics, $profile);
  echo '&lt;pre&gt;';
  print_r($results);
  echo '&lt;br&gt;';
  </code></pre>