Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不在对象上下文中时使用$this错误_Php_Oop_Google Analytics Api - Fatal编程技术网

Php 不在对象上下文中时使用$this错误

Php 不在对象上下文中时使用$this错误,php,oop,google-analytics-api,Php,Oop,Google Analytics Api,在尝试执行脚本时,我遇到了一个致命的PHP错误:在不在对象上下文中时使用$this脚本通过API从Google Analytics收集数据,然后将其显示在页面上 下面是脚本终止的代码段: $records = $this->getAnalyticRecords ( date ( 'Y-m-d', $startTime ), date ( 'Y-m-d', $endEnd ), 'ga:date', 'ga:visitors,ga:newVisits,ga:visits,ga:pagevie

在尝试执行脚本时,我遇到了一个致命的PHP错误:
在不在对象上下文中时使用$this
脚本通过API从Google Analytics收集数据,然后将其显示在页面上

下面是脚本终止的代码段:

$records = $this->getAnalyticRecords ( date ( 'Y-m-d', $startTime ), date ( 'Y-m-d', $endEnd ), 'ga:date', 'ga:visitors,ga:newVisits,ga:visits,ga:pageviews,ga:timeOnPage,ga:bounces,ga:entrances,ga:exits' );
getAnalyticsRecords由以下设置:

function getAnalyticRecords($startDate, $endDate, $dimensions, $metrics, $sort = '', $maxResults = '') {

    $url = 'https://www.google.com/analytics/feeds/data';
    $url .= "?ids=" . $this->profile;
    $url .= "&start-date=" . $startDate;
    $url .= "&end-date=" . $endDate;
    $url .= "&dimensions=" . $dimensions;
    $url .= "&metrics=" . $metrics;
    if (! empty ( $sort )) {
        $url .= "&sort=" . $sort;
    }
    if (! empty ( $maxResults )) {
        $url .= "&max-results=" . $maxResults;
    }
    if (($feedData = $this->fetchFeed ( $url )) === FALSE) {
        return array ();
    }
    $doc = new DOMDocument ( );
    $doc->loadXML ( $feedData );
    $results = array ();

    $aggregates = $doc->getElementsByTagName ( 'aggregates' );
    foreach ( $aggregates as $aggregate ) {
        $metrics = $aggregate->getElementsByTagName ( 'metric' );
        foreach ( $metrics as $metric ) {
            $results ['aggregates'] ['metric'] [$metric->getAttribute ( 'name' )] = $metric->getAttribute ( 'value' );
        }
    }

    $entries = $doc->getElementsByTagName ( 'entry' );
    foreach ( $entries as $entry ) {
        $record = array ();
        $record ["title"] = $entry->getElementsByTagName ( 'title' )->item ( 0 )->nodeValue;
        $dimensions = $entry->getElementsByTagName ( 'dimension' );
        foreach ( $dimensions as $dimension ) {
            $record ['dimension'] [$dimension->getAttribute ( 'name' )] = $dimension->getAttribute ( 'value' );
        }
        $metrics = $entry->getElementsByTagName ( 'metric' );
        foreach ( $metrics as $metric ) {
            $record ['metric'] [$metric->getAttribute ( 'name' )] = $metric->getAttribute ( 'value' );
        }
        $results ['entry'] [] = $record;
    }
    return $results;
}

你应该听听这个错误。当不在类中时,您正在使用
$this


尝试将代码封装在类中,或者忽略
$this->
。改用
global

既然给出的第二个代码在一个类中,我如何重新引入该类?可能
$xfplugin=new googleAnalyticsPlugin@ct2k7如果getAnalyticRecords()是一个名为googleAnalyticsPlugin的类的方法,则是<代码>$xfplugin=新的googleAnalyticsPlugin$记录=$xfplugin->getAnalyticRecords(…)谢谢timdiv和Borelid:)