2015-12-02 21 views
0

我拉從Adobe Analytics(分析)的報告,我試圖將結果保存在一個CSV文件CSV文件。這裏是我的代碼的描述:一個JSON變量保存到使用fputcsv

<?php 
include_once('/path/SimpleRestClient.php'); 

// Creation of csv filepath 
$adobe_file = $sql_path = '/path/adobe.csv'; 

// List creation that will be updated with the fields and be put into my CSV file 
$list = array 
    (
     array('page','page_views') // headers 
    ); 

class API 
{ 
    // Get it form the platform after logging in, User icon, settings 
    private $username  = "XXXXX"; 
    private $shared_secret = "XXXXX"; 
    private $postURL  = "https://api3.omniture.com/admin/1.4/rest/?method="; 

    private function _request($m, $o) {  
     $nonce  = md5(uniqid(php_uname('n'), true)); 
     $nonce_ts = date('c'); 
     $digest  = base64_encode(sha1($nonce . $nonce_ts . $this->shared_secret)); 

     $rc   = new SimpleRestClient(); 
     $rc   -> setOption(CURLOPT_HTTPHEADER, array("X-WSSE: UsernameToken Username=\"$this->username\", PasswordDigest=\"$digest\", Nonce=\"$nonce\", Created=\"$nonce_ts\"")); 
     //var_dump($o); 
     $rc   -> postWebRequest($this->postURL . $m, $o);  
     return $rc; 
    } 

    private function _queue($o) 
    { 
     return $this -> _request('Report.Queue', $o); 
    } 

    private function _get($o, $sleep = 120) 
    { 
     $counter = 0; 
     do 
     { 
      if($counter>0){sleep($sleep);} 
      $rc = $this -> _request('Report.Get', $o);  
      $counter++; 
     }while($rc -> getStatusCode() == 400 && json_decode($rc->getWebResponse())->error == 'report_not_ready'); 

     return $rc; 
    } 

    public function get($rsid, $dates, $granularity, $metrics, $elements, $segments) 
    { 

     if($granularity == 'week') 
     { 
      $date = '"dateFrom":"' . $dates['from'] . '", 
         "dateTo":"' . $dates['to'] . '", 
         "dateGranularity":"week",'; 
     } 
     else 
     { 
      $date = '"date":"' . $dates['from'] . '", 
         "dateGranularity":"' . $granularity . '",'; 
     } 

     $reportDescription = '{ 
            "reportDescription": 
            { 
             "reportSuiteID":"' . $rsid . '", 
             ' . $date . ' 
             "metrics":' . $metrics . ', 
             "elements":' . $elements . ', 
             "segments":' . $segments . ' 
            } 
           }'; 

     $queuedReport = $this->_queue($reportDescription); 

     if($queuedReport -> getStatusCode() == 200) 
     { 
      return $this -> _get($queuedReport -> getWebResponse()); 
     } 
     return $queuedReport; 
    } 

    public function get_async($wait = 15) 
    { 

     $reportDescription  ='{ 
      "reportDescription":{ 
       "reportSuiteID":"XXXXX", 
       "dateFrom":"2015-11-01", 
       "dateTo":"2015-11-30", 
       "metrics":[{"id":"pageviews"}], 
       "elements":[{"id":"page","top":"25"}] 
      } 
      }'; 

     $queuedReport = $this->_queue($reportDescription); 

     if($queuedReport -> getStatusCode() == 200) 
     { 
      $return = $this -> _get($queuedReport -> getWebResponse(),$wait); 
     } 
     else 
     { 
      $return = $queuedReport; 
     } 

     return $return; 
    } 
} 

$myclass = new API; 
$output = $myclass->get_async(); 

現在我想將Adobe Analytics報告的輸出傳遞給csv文件。通常,我使用此代碼:

$fp = fopen($adobe_file, 'w'); 

while ($row = mysql_fetch_row($output)) { 
     $marks = json_decode($row['responsesJSON'],true); 
     unset($row['responsesJSON']); 
     fputcsv($fp, array_merge(array_values($row),array_values($marks))); // 
    } 

    fclose($fp); 

但在這種情況下,輸出是一個JSON變量。 ,因爲列表是空的我的csv文件是空。用1.3我可以輕鬆做到。

回答

0

刪除功能更好,因爲沒有必要使用一個以上的PHP文件。所以,代碼工作如下:

<?php 

include_once('path/SimpleRestClient.php'); 

// Creation of CSV filepath 
$adobe_file = 'path/Adobe.csv'; 

// List creation that will be updated with the fields and be put into my CSV file 
$list = array 
    (
     array('page','page_views') // headers // ADD or DELETE metrics # 
    ); 

function GetAPIData($method, $data) 
{ 
    $username  = "XXXXX"; 
    $shared_secret = "XXXXX"; 
    $postURL  = "https://api3.omniture.com/admin/1.4/rest/?method="; 

    // Nonce is a simple unique id to each call to prevent MITM attacks. 
    $nonce  = md5(uniqid(php_uname('n'), true)); 
    // The current timestamp in ISO-8601 format 
    $nonce_ts = date('c'); 
    // The Password digest is a concatenation of the nonce, its timestamp and your password (from the same location as your username) which runs through SHA1 and then through a base64 encoding  
    $digest  = base64_encode(sha1($nonce . $nonce_ts . $shared_secret)); 

    $rc = new SimpleRestClient();  
    $rc -> setOption(CURLOPT_HTTPHEADER, array("X-WSSE: UsernameToken Username=\"$username\", PasswordDigest=\"$digest\", Nonce=\"$nonce\", Created=\"$nonce_ts\"")); 
    //var_dump($o); 

    $rc -> postWebRequest($postURL .$method, $data);  
    return $rc; 
} 

$method = 'Report.Queue'; 
$data  =' 
      { 
       "reportDescription": 
       { 
       "reportSuiteID":"XXXXX", 
       "dateFrom":"2015-11-01", 
       "dateTo":"2015-11-30", 
       "metrics":[{"id":"pageviews"}], 
       "elements":[{"id":"page","top":"25"}] 
       } 
      }'; 

$rc=GetAPIData($method, $data); 

if($rc -> getStatusCode() == 200) // status code 200 is for 'ok' 
{ 
    $counter = 0; 
    do 
    { 
     if($counter>0){sleep($sleep = 120);} 
     $return = GetAPIData('Report.Get', $rc -> getWebResponse()); 
     $counter++; 
    }while($return -> getStatusCode() == 400 && json_decode($return->getWebResponse())->error == 'report_not_ready'); // status code 400 is for 'bad request' 

     // valto 2 
    $json=json_decode($return->getWebResponse()); 

    foreach ($json->report->data as $el) 
    { 
     echo $el->name.":".$el->counts[0]."\n"; 

     // Adding the data in the CSV file without overwriting the previous data 
     array_push($list, array($el->name, $el->counts[0])); 
    } 

// xe valto 2 
} 
else 
{ 
    $return = $queuedReport; 
} 

$fp = fopen($adobe_file, 'w'); 

foreach ($list as $fields) 
{ 
    // save rows to the CSV file 
    fputcsv($fp, $fields); 
} 

fclose($fp); 

?>