Exemplo n.º 1
0
 /**
  * Execute a pre-defined report and return the data.  Optionally pass arguments to the report.
  * Note that readReport reverts to asynchronous mode when the report exceeds a fixed amount of time.
  *
  * @param string                        $reportName The report name
  * @param api_session                   $sess       Connected api_session object
  * @param array|api_readReportArguments $arguments  Should be array of arguments ['key'] => $arg .
  * @return simpleXmlElement[]
  * @throws Exception
  */
 public static function readReport($reportName, api_session $sess, api_readReportArguments $arguments = null)
 {
     $runXml = new SimpleXMLElement("<readReport/>");
     $runXml->addAttribute('returnDef', 'false');
     $runXml->addChild('report', $reportName);
     $runXml->addChild('pagesize', self::DEFAULT_PAGESIZE);
     $runXml->addChild('returnFormat', 'xml');
     if (!is_null($arguments)) {
         $arguments->addAsElement($runXml);
     }
     $runXml->addChild('waitTime', 5);
     $runXmlString = api_util::xmlElementToSnippet($runXml);
     $response = api_post::post($runXmlString, $sess);
     $responseXml = new simpleXmlElement($response);
     if ($responseXml === false) {
         throw new Exception("Invalid XML response in readReport.");
     }
     // Did we get the results or did we get a handle?
     $reportHandle = $responseXml->operation->result->data->report_results;
     $reportId = (string) $reportHandle->REPORTID;
     $reportStatus = (string) $reportHandle->STATUS;
     $allResults = array();
     $reportResult = null;
     $readMoreResponse = null;
     // Ping every 5 seconds until the report is ready
     while ($reportStatus == 'PENDING') {
         sleep(5);
         $reportResult = api_post::readMoreReport($reportId, $sess);
         $reportStatus = (string) $reportResult->report->data->STATUS;
         if ($reportStatus == '') {
             // weird.  if the status is DONE, it shows up in data->report->status
             $reportStatus = (string) $reportResult->report->STATUS;
         }
     }
     // read the report results page by page until we have all the records
     $allResults = api_post::gatherReportResults($reportResult, $allResults);
     $recordsLeft = api_post::getRecordsLeftAttribute($reportResult);
     while ($recordsLeft != 0) {
         $reportResult = api_post::readMoreReport($reportId, $sess);
         $allResults = api_post::gatherReportResults($reportResult, $allResults);
         $recordsLeft = api_post::getRecordsLeftAttribute($reportResult);
     }
     return $allResults;
 }