【问题标题】:Google Analytics API - PHP Object vs Array?Google Analytics API - PHP 对象与数组?
【发布时间】:2014-01-15 09:53:37
【问题描述】:

当我请求 API 值时,$results 返回一个数组,但 google 在他们的示例中使用 $results 作为对象?

代码位于:https://developers.google.com/analytics/devguides/reporting/realtime/v3/reference/data/realtime/get#examples

我总是得到一个

致命错误:在非对象上调用成员函数 getKind()

他们是否更改了库并且他们自己的示例不再工作,或者我这边有什么错误?但到目前为止我还没有更改代码,除非 $ optparams 值。

PS:到目前为止一切正常,只是对象/数组问题。

我得到每个print_r($results)

Array ( 
    [kind] => analytics#realtimeData
    [id] => https://www.googleapis.com/analytics/v3/data/realtime?ids=ga:**censored**&dimensions=ga:medium&metrics=ga:activeVisitors
    [query] => Array (
        [ids] => ga:**censored**
        [dimensions] => ga:medium
        [metrics] => Array (
            [0] => ga:activeVisitors
        )
        [max-results] => 1000
    )
    [totalResults] => 8
    [selfLink] => https://www.googleapis.com/analytics/v3/data/realtime?ids=ga:**censored**&dimensions=ga:medium&metrics=ga:activeVisitors
    [profileInfo] => Array (
        [profileId] => **censored**
        [accountId] => **censored**
        [webPropertyId] => **censored**
        [internalWebPropertyId] => **censored**
        [profileName] => **censored**
        [tableId] => realtime:**censored**
    )
    [columnHeaders] => Array (
        [0] => Array (
            [name] => rt:medium
            [columnType] => DIMENSION
            [dataType] => STRING
        )
        [1] => Array (
            [name] => ga:activeVisitors
            [columnType] => METRIC
            [dataType] => INTEGER
        )
    )
    [totalsForAllResults] => Array (
        [ga:activeVisitors] => 752
    )
    [rows] => Array (
        [0] => Array (
            [0] => DIRECT [1] => 283
        )
        [1] => Array (
            [0] => E-Mail [1] => 5
        )
        [2] => Array (
            [0] => ORGANIC [1] => 287
        )
        [3] => Array (
            [0] => REFERRAL [1] => 92
        )
        [4] => Array (
            [0] => SOCIAL [1] => 49
        )
        [5] => Array (
            [0] => cpc [1] => 23
        )
        [6] => Array (
            [0] => e-mail [1] => 8
        )
        [7] => Array (
            [0] => facebook [1] => 5
        )
    )
)

【问题讨论】:

  • “在非对象上”表示您尝试访问的对象是NULL。您确定要取回数据吗?
  • 代码在可用的 url 下(不想炸毁网站),它是一样的.. 是的,如果我使用 print_r($results) 我得到实时值..
  • 那么,print_r 显示了什么?也许您只是访问错误?
  • 我用 print_r 结果编辑了帖子。我不确定,并没有真正改变访问周围的任何东西。直到以下部分 $analytics 仍然是一个对象并且 $results 也应该成为一个对象?:尝试 { $results = $analytics->data_realtime->get('ga:xxxxx', 'ga:activeVisitors', $optParams);在每个步骤中使用 gettype() 对其进行多次检查..

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


【解决方案1】:

您使用 setUseObjects 方法在客户端上设置它。例如:

$client->setUseObjects(true);

有关项目托管,请参阅 Google_Client.php#384

但是,目前看来library on GitHub 的新 alpha 版本不再支持此功能。

【讨论】:

  • 谢谢!那是问题/解决方案->通过设置setUseObjects值失败..在config.php中将默认设置为true后看到它..
猜你喜欢
  • 1970-01-01
  • 2016-10-19
  • 2018-07-25
  • 1970-01-01
  • 2019-01-27
  • 2012-11-20
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多