1
嘗試執行腳本時出現致命PHP錯誤:Using $this when not in object context
該腳本通過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;
}
由於給出的第二個代碼是在一個類中,我該如何重新引入該類?可能是'$ xfplugin = new googleAnalyticsPlugin;' – bear 2010-08-12 23:52:03
@ ct2k7如果getAnalyticRecords()是一個名爲googleAnalyticsPlugin的類的方法,那麼是的。 '$ xfplugin = new googleAnalyticsPlugin; $ records = $ xfplugin-> getAnalyticRecords(...);' – timdev 2010-08-12 23:57:02
感謝timdiv和Borealid :) – bear 2010-08-13 00:03:51