【发布时间】:2017-01-28 12:29:41
【问题描述】:
我正在尝试提取用户登录 G Suite 资产的次数信息,以便创建图表。 (我知道这个功能是原生的,但是希望通过 API 调用来实现,因为我们以后需要其他数据)
我卡住的地方是处理响应。我的代码目前如下所示:
<?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('administrator@mydomain.com');
// 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]);
}
}
?>
现在返回以下内容:
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 ( ) )
我遇到的问题是,我发现通过键 ID 调用参数是没有用的,因为它似乎会根据完整的 API 结果不时发生变化。使用key ID调用api的代码如下:
$data->getparameters()[23]->getintValue()
我发现虽然上面的密钥 ID 目前显示为 23,但这可以改变。出于这个原因,我决定尝试按我需要的数据名称进行搜索,即:accounts:num_1day_logins
问题是返回数据的格式,当我打印$data->getparameters()[23] 的数组时,我得到以下信息:
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 ( ) )
如何搜索name 并获取数组键?
我会发布结果:$data->getparameters() 但是我担心数据隐私。
有人搞定这个吗?请有人帮忙。如果需要更多信息,请告诉我。
【问题讨论】:
标签: php arrays google-api-php-client google-api-client google-client