动态创建对象实例名称

时间:2018-09-10 22:18:23

标签: php api object analytics

我有一系列星期在其他地方动态创建(这是print_r转储):

  array:28 [▼
  “week1” => “2018-03-01”
  “week2” => “2018-03-08”
  “week3” => “2018-03-15”
  “week4” => “2018-03-22”
  “week5” => “2018-03-29”
  “week6” => “2018-04-05”
  “week7” => “2018-04-12”
  “week8” => “2018-04-19”
  “week9” => “2018-04-26”
  “week10” => “2018-05-03”
  “week11” => “2018-05-10”
  “week12” => “2018-05-17”
  “week13” => “2018-05-24”
  “week14” => “2018-05-31”
  “week15” => “2018-06-07”
  “week16” => “2018-06-14”
  “week17” => “2018-06-21”
  “week18” => “2018-06-28”
  “week19” => “2018-07-05”
  “week20” => “2018-07-12”
  “week21” => “2018-07-19”
  “week22” => “2018-07-26”
  “week23” => “2018-08-02”
  “week24” => “2018-08-09”
  “week25” => “2018-08-16”
  “week26” => “2018-08-23”
  “week27” => “2018-08-30”
  “week28” => “2018-09-06”
]

对于这两个星期中的每个星期,我都想创建一个Google_Service_AnalyticsReporting_DateRange对象的实例,以便可以在同一批调用中查询多个日期范围。

但是,根据Google Analytics(分析)Reporting API V4,我必须创建具有不同名称的实例:

Samples  |  Analytics Reporting API v4  |  Google Developers

特别是:

$march = new Google_Service_AnalyticsReporting_DateRange();
$march->setStartDate(“2015-03-01”);
$march->setEndDate(“2015-03-31”);

$January = new Google_Service_AnalyticsReporting_DateRange();
$January->setStartDate(“2015-01-01”);
$January->setEndDate(“2015-01-31”);

我的问题是我无法弄清楚是否有可能循环遍历几周数组以动态创建多个实例(名称不同)。这是我的方法:

private function get_timeline_report($analytics, $campaign_start_date, $dates, $expression=null, $alias=null, $filter=null, User $user)
{
    $dates = (array_values($dates));

    foreach ($dates as $key => $value)
    {
        // todo - need to get date and give it a name (week1 etc - see http://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.newx)
        // Create the DateRange object.
        $date_ranges[$key] = new Google_Service_AnalyticsReporting_DateRange();
        $date_ranges[$key]->setStartDate($campaign_start_date);
        $date_ranges[$key]->setEndDate($value);
    }

    // Create the Metrics object.
    $sessions = new Google_Service_AnalyticsReporting_Metric();
    $sessions->setExpression($expression);
    $sessions->setAlias($alias);

    // Create the ReportRequest object.
    $request = new Google_Service_AnalyticsReporting_ReportRequest();
    $request->setViewId($user->ga_view_id);
    $request->setDateRanges($date_ranges);
    $request->setMetrics($sessions);
    $request->setFiltersExpression($filter);

    $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
    $body->setReportRequests(array($request));

    return $analytics->reports->batchGet($body);
}

是否可以动态创建实例名称,如果可以,如何创建?当前代码返回此Google服务异常:

(1/1) Google_Service_Exception
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"week1\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week2\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week3\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week4\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week5\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week6\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week7\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week8\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week9\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week10\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week11\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week12\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week13\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week14\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week15\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week16\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week17\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week18\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week19\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week20\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week21\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week22\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week23\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week24\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week25\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week26\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week27\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week28\" at 'report_requests[0].date_ranges[0]': Cannot find field.",
"errors": [
{
"message": "Invalid JSON payload received. Unknown name \"week1\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week2\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week3\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week4\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week5\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week6\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week7\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week8\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week9\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week10\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week11\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week12\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week13\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week14\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week15\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week16\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week17\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week18\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week19\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week20\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week21\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week22\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week23\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week24\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week25\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week26\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week27\" at 'report_requests[0].date_ranges[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"week28\" at 'report_requests[0].date_ranges[0]': Cannot find field.",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT"
}
}

1 个答案:

答案 0 :(得分:2)

您问了一个错误的问题,因为您的错误实际上并非来自对象实例的名称。 在多个日期范围内的google示例中,我们可以注意到:

 $request->setDateRanges(array($march, $january));

您可能会看到,这个数组根本没有关联。因此我们可以得出结论,Google Api不会使用所有实例名称,而只会使用dateRange实例本身。因此,如果您使用第一个代码代替:

foreach ($dates as $key => $value)
    {
        // Create the DateRange object.
        $date_ranges[$key] = new Google_Service_AnalyticsReporting_DateRange();
        $date_ranges[$key]->setStartDate($campaign_start_date);
        $date_ranges[$key]->setEndDate($value);
    }

您使用类似的内容:

$i=0;
foreach ($dates as $key => $value)
{
    // Create the DateRange object.
    $date_ranges[$i] = new Google_Service_AnalyticsReporting_DateRange();
    $date_ranges[$i]->setStartDate($campaign_start_date);
    $date_ranges[$i]->setEndDate($value);
    $i++;
}
unset($i,$key,$value);