2010-03-10 24 views
1

我正在編寫腳本以輸出Google AnalyticsAPI數據並使用Google Charts API將其插入到條形圖中。當我在URL中有這樣一個字符串時,我會得到期望的結果。當變量爲08或09時出現「致命錯誤:無法將字符串偏移量作爲數組使用」

gaFeedData.php?y[]=2009&y[]=2010&m[]=1&m[]=2 

然而,當我在URL下面的字符串,我得到一個錯誤:致命錯誤:無法使用字符串作爲上線56

在gaFeedData.php數組offset(m個[] = 8是具有m [] = 9。不管什麼原因,米[] = 10,M [] = 11且m [] = 12工作互換。)

gaFeedData.php?y[]=2009&y[]=2010&m[]=8 

還要注意,GA 是否有這些月份的數據。

我的PHP代碼如下,認證信息遺漏:

_config.php:

<?php 

$accountType = 'GOOGLE'; // DONT EDIT!    // Account type 
$source   = 'report'; // DONT EDIT!    // Application name 
$accountName = '[email protected]';      // User's email 
$accountPass = 'password';       // User's password 

$clientName  = 'useratgmail';     // Client's name 

$goalid   = $_GET['goal']; 
$startdate  = $_GET['startdate']; 
$enddate  = $_GET['enddate']; 

$y[0]   = 0; 
$m[0]   = 0; 

$y    = $_GET["y"]; 
$m    = $_GET["m"]; 



$URIAuth  = 'https://www.google.com/accounts/ClientLogin'; 
$URIFeedAcct = 'https://www.google.com/analytics/feeds/accounts/default?prettyprint=true'; 
$URIFeedData = 'https://www.google.com/analytics/feeds/data?prettyprint=true'; 

?> 

gaFeedData.php:

<?php 

include("_config.php"); 

$TABLE_ID = 'ga:11111111'; 
foreach ($y as $yy) { 
    if ($yy%400==0)  $leapyear='1'; 
    elseif ($yy%100== 0) $leapyear='0'; 
    elseif ($yy%4==0)  $leapyear='1'; 
    else     $leapyear='0'; 
    $month = array(); 
    $month[01][dfirst] = $yy.'-01-01'; 
    $month[01][dlast] = $yy.'-01-31'; 
    $month[02][dfirst] = $yy.'-02-01'; 
    if ($leapyear=='1') $month[02][dlast] = $yy.'-02-29'; 
    else     $month[02][dlast] = $yy.'-02-28'; 
    $month[03][dfirst] = $yy.'-03-01'; 
    $month[03][dlast] = $yy.'-03-31'; 
    $month[04][dfirst] = $yy.'-04-01'; 
    $month[04][dlast] = $yy.'-04-30'; 
    $month[05][dfirst] = $yy.'-05-01'; 
    $month[05][dlast] = $yy.'-05-31'; 
    $month[06][dfirst] = $yy.'-06-01'; 
    $month[06][dlast] = $yy.'-06-30'; 
    $month[07][dfirst] = $yy.'-07-01'; 
    $month[07][dlast] = $yy.'-07-31'; 
    $month[08][dfirst] = $yy.'-08-01'; 
    $month[08][dlast] = $yy.'-08-31'; 
    $month[09][dfirst] = $yy.'-09-01'; 
    $month[09][dlast] = $yy.'-09-30'; 
    $month[10][dfirst] = $yy.'-10-01'; 
    $month[10][dlast] = $yy.'-10-31'; 
    $month[11][dfirst] = $yy.'-11-01'; 
    $month[11][dlast] = $yy.'-11-30'; 
    $month[12][dfirst] = $yy.'-12-01'; 
    $month[12][dlast] = $yy.'-12-31'; 

    foreach ($m as $mm) { 
     sleep(0.2); 
     $ch = curl_init($URIFeedData.'&ids='.$TABLE_ID.'&start-date='.$month[$mm][dfirst].'&end-date='.$month[$mm][dlast].'&metrics=ga:visits,ga:visitors,ga:pageviews,ga:timeOnSite'.'&alt=json'); 
     $fp = fopen("$clientName.data.feed", "w"); 

     $accountAuth = exec('awk /Auth=.*/ '.$clientName.'.auth'); 

     curl_setopt($ch, CURLOPT_FILE, $fp); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: GoogleLogin $accountAuth","GData-Version: 2")); 

     curl_exec($ch); 
     curl_close($ch); 
     fclose($fp); 

     $jsonfile = fopen("$clientName.data.feed", "r"); 
     $jsondata = fread($jsonfile, filesize("$clientName.data.feed")); 
     $output = json_decode($jsondata, 512); 
     $data[$yy][$mm][visits] = $output[feed]["dxp\$aggregates"]["dxp\$metric"][0][value]; 
     echo $data[$yy][$mm][visits].", "; 
    } 
} 

?> 
+1

哪條線是56? – abeger 2010-03-10 19:50:08

+0

第56行的gaFeedData.php是什麼? – Josh 2010-03-10 19:50:23

+0

$ data [$ yy] [$ mm] [visits] = $ output [feed] [「dxp \ $ aggregates」] [「dxp \ $ metric」] [0] [value]; – sjstrutt 2010-03-10 19:56:34

回答

5

你需要把你的兩位數月份名稱變爲引號:

$month["07"][dfirst] = $yy.'-07-01'; 

否則,PHP將解釋數as an octal value

Formally, the structure for integer literals is: 

decimal  : [1-9][0-9]* 
      | 0 

hexadecimal : 0[xX][0-9a-fA-F]+ 

octal  : 0[0-7]+ 
... 

我認爲在聲明數組成員你做的方式,八進制將導致被設置了錯誤的鑰匙。

0

我相信@Pekka是正確的。我想提一下,無論你在哪裏,都有[dfirst][dlast],你應該用引號括起索引。所以:

if ($leapyear=='1') $month[02][dlast] = $yy.'-02-29'; 
    else     $month[02][dlast] = $yy.'-02-28'; 

應該是:

if ($leapyear=='1') $month['02']['dlast'] = $yy.'-02-29'; 
    else     $month['02']['dlast'] = $yy.'-02-28'; 
相關問題