Search code examples
phparraysgoogle-api-php-clientgoogle-api-clientgoogle-client

Handling responses with Google G Suite API PHP Library


I am trying to pull the information of how many times our users have logged in to our G Suite estate so I can create a graph. (I know this functionality is native however wish to do it via API calls as there is other data we will need at a later date)

Where I am stuck is with handling the response. My code currently looks like this:

<?php
require_once __DIR__ . '/vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=credentials.json');
define('SCOPES', implode(' ', array(
  Google_Service_Reports::ADMIN_REPORTS_USAGE_READONLY,
  Google_Service_Reports::ADMIN_REPORTS_AUDIT_READONLY)
));


$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(SCOPES);
$client->setAccessType('offline');
$client->setSubject('[email protected]');

// Get the API client and construct the service object.
$service = new Google_Service_Reports($client);

// API CALL

$date = '2017-01-19';
$optParams = array(

);


$results = $service->customerUsageReports->get(
    $date, $optParams);


if (count($results->getusageReports()) == 0) {
  print "No logins found.\n";
} else {
  foreach ($results->getusageReports() as $data) {

    print("Number of logins (past 24 hours): " . $data->getparameters()[23]->getintValue() . "<br />");

    echo "<br />";


    // Use this for checking array values
    print_r($data->getparameters()[23]);



  }
}

?>

Now this returns the following content:

Number of logins (past 24 hours): 2

Google_Service_Reports_UsageReportParameters Object ( [collection_key:protected] => msgValue [boolValue] => [datetimeValue] => [intValue] => 2 [msgValue] => [name] => accounts:num_1day_logins [stringValue] => [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) )

The problem I have is that, I have found that by calling the parameter by key ID is useless as it appears to change from time to time depending on the full API results. The code which calls the api using the key ID is as follows:

$data->getparameters()[23]->getintValue()

I found that although the key ID above is shown as 23 at current, this can change. For this reason I decided to try and search by the name of the data I need which is: accounts:num_1day_logins

The problem is the format that the data is returned in, when I print the array for $data->getparameters()[23] then I get the following:

Google_Service_Reports_UsageReportParameters Object ( [collection_key:protected] => msgValue [boolValue] => [datetimeValue] => [intValue] => 2 [msgValue] => [name] => accounts:num_1day_logins [stringValue] => [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) )

How would I search for the name and get the array key?

I would post the results of: $data->getparameters() however am worried about data privacy.

Has anyone got this working? Please can someone help assist. If there is any more information needed please let me know.


Solution

  • Ok so the problem here is that Google's object that is returned isn't as consistent as they like to tell you it is.

    You'll need to do another foreach loop with an if statement to pull the object you need (basically it returns an object containing multiple objects, we want to pull a specific object by name)

    The code below should do this, you can see I've added an additional foreach() loop to loop through the objects inside the parent object and the if statement makes sure we only deal with the object we want.

    if (count($results->getusageReports()) == 0) {
      print "No logins found.\n";
    } else {
      foreach ($results->getusageReports() as $data) {
    
        foreach ($data->getparameters() as $pizza) {
    
            if ($pizza->name == "accounts:num_1day_logins"){
    
              var_dump ($pizza);
    
            }
    
        }
    
    
      }
    }
    

    I can't gurantee it's the cleanest solution but should work!