除了我拥有的这种简化方式之外,是否可以多次实例化同一个类?
include ("php/functions.php");
date_default_timezone_set('Europe/Lisbon');
require 'php/gapi.class.php';
$ga1 = new gapi('XXXX@gmail.com','XXXX');
$ga2 = new gapi('XXXX@gmail.com','XXXX');
$ga3 = new gapi('XXXX@gmail.com','XXXX');
$ga1->requestReportData( $report_id = XXXX, $dimension = array('browser'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visitors', $filter = 'browser == Firefox || browser == Chrome || browser == Internet Explorer || browser == Safari || browser == Opera && visits > 10', $start_date = '2012-10-17' );
$ga2->requestReportData( $report_id = XXXX, $dimension = array('country'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visits', $filter = 'country != (not set) && visits > 3', $start_date = '2012-10-17', null, null, 10 );
$date2 = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-15, date("Y")));
$today = date('Y-m-d');
$ga3->requestReportData($report_id = XXXX, $dimension = array('date'), $metrics = array('pageviews','visits'), $sort_metric = array('date') , $filter = '',$date2,$today, null,15);
PS:这段代码按预期工作......我只是想知道是否有更有效的方法来实现这一点......
编辑:要回答对所提供的某些解决方案提出的一些问题,下面是同一个文件几百行后面的应用程序示例:
<h2 class="verde">By country (10 relvant results)</h2>
[useless code ommited...]
<tbody>
<?php
$tp = 0;
foreach($ga2->getResults() as $r2){
if($tp==10) break;
?>
<tr>
<td><?php echo $r2 ?></td>
<td><?php echo $r2->getVisitors() ?></td>
<td><?php echo $r2->getVisits() ?></td>
<td><?php echo $r2->getPageviews() ?></td>
</tr>
<?php
$tp++;
}
?>
<tr style="font-weight:700">
<td><strong>TOTAIS</strong></td>
<td><strong><?php echo $ga2->getVisitors() ?></strong></td>
<td><strong><?php echo $ga2->getVisits() ?></strong></td>
<td><strong><?php echo $ga2->getPageviews() ?></strong></td>
</tr>
</tbody>
我不确定我是否可以在这里发布链接......但这段代码可以在http://jb.utad.pt/gapi/上看到