2014-09-13 62 views
1

我在AWQL此查詢,我得到使用ReportUtils::DownloadReportWithAwql轉換萬分之一到貨幣AWQL CSV報告與Adwords API

select Date, Clicks, Cost from ACCOUNT_PERFORMANCE_REPORT during LAST_30_DAYS 

我需要將成本數據的CSV從轉換成CSV格式的響應微分到賬戶中的貨幣(Cost/1000000)。

此外,我需要能夠以任何成本數據轉換使用任何 AWQL查詢的響應,例如解決方案必須爲這個查詢還工作:

SELECT CampaignName, KeywordText, Cost, CostPerConversion, QualityScore FROM KEYWORDS_PERFORMANCE_REPORT DURING LAST_7_DAYS 

由於v201406的,returnMoneyInMicros標題不再有效,並且值總是以微格形式返回。 https://developers.google.com/adwords/api/docs/guides/reporting-concepts#money

這是我的第一個問題在stackoverflow。

+0

@drep我做2個查詢和結果是[這裏](https://gist.github.com/mayeco/1904e3d8243c62a129ea)現在想象你在$變量2,一個$變量1和cost2 COST1。如何使用相同的函數將變量中的成本和成本/轉換列除以1000000? – mayeco 2014-09-13 04:44:01

回答

0

最後我做到了,對我很好。

//data is a string with data in micros in csv format 
    $data = $this->DownloadCriteriaReportWithAwql($awql); 

    //start micros conversion 
    $count = 0; 
    $costpos = array(); 
    $newarray = array(); 
    foreach(preg_split("/((\r?\n)|(\r\n?))/", $data) as $line){ 

     $linearray = str_getcsv($line); 

     if($count == 0) { 
      //adwords report title 
      $newarray[] = $linearray; 
      $count++; 
      continue; 
     } 

     if($count == 1) { 
      //columns report header 
      $postvalue = 0; 

      foreach($linearray as $value){ 
       if (strpos($value,'Cost') !== false) { 
        $costpos[] = $postvalue; 
       } 
       $postvalue++; 
      } 

      $newarray[] = $linearray; 
      $count++; 
      continue; 
     } 

     if(!empty($costpos)) { 

      foreach($costpos as $costpostval){ 

       if(isset($linearray[$costpostval])) { 
        $linearray[$costpostval] = $linearray[$costpostval]/1000000; 
       } 

      } 
     } 

     $newarray[] = $linearray; 
     $count++; 
    } 


    $tmpfname = tempnam(sys_get_temp_dir(), "FOO"); 
    $outstream = fopen($tmpfname, "r+"); 
    foreach($newarray as $newline){ 
     fputcsv($outstream, $newline); 
    } 
    fclose($outstream); 
    //end micros conversion - $tmpfname temp file with cost in currency csv formated