Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SoapClient fails after one request

Tags:

php

soap

I have a PHP script that uses SoapClient to access an API. After connecting, if I just make one request, it works as expected but when I try to make a second request with the same SoapClient object, i get a Bad Request error.

I believe the issue may have to do with my server configuration because the same PHP code works fine in another computer with an older version of PHP but on my test and production servers which have been updated to PHP 5.6 both experience this error.

Code:

    <?php

class ReportAPI
{
  protected $reportDataObject = null;

  public function __construct($url = 'URL', $username = 'username', $password = 'pass') {
    $this->client = new SoapClient($url , array('login' => $username, 'password' => $password, 'keep_alive' => true));
  }
  public function getReportData($reportID) {
    return $this->getDataObject($reportID)->data;
  }
  public function getReportCount($reportID) {
    return $this->getDataObject($reportID)->result_count;
  }
  public function runReport($reportID) {
    $newID = $this->client->runReport($reportID);
    $counter = 0;
    while($this->client->checkReportRun($newID) != 'complete'){
      if($counter > 2) {
        throw new Exception('Report Time Out');
      }
      sleep(5);
      $counter++;
    }
    $this->getDataObject($newID);
    return $newID;
  }

  public function functest() {
    $v0 = $this->getDataObject('id')->result_count;
    return $this->getDataObject('id')->data;
  }

  public function getLatestID($reportID) {
    $runlist = $this->client->getReportRunList($reportID);
    return $runlist[0][0];
  }

  public function getLatestReportData($reportID) {
    return $this->getReportData($this->getLatestID($reportID));
  }

  public function getTitles($reportID) {
    return $this->getDataObject($reportID)->columns;
  }

  public function getReportRunTime($reportID) {
    $runlist = $this->client->getReportRunList($reportID);
    return strtotime($runlist[0][2]);
  }

  public function getRawReport($reportID) {
    return $this->client->getReportDataObject($reportID);
  }

  protected function getDataObject($reportID) {
    if(!isset($this->reportDataObject) || $this->reportDataObject->report_run_id != $reportID) {
      $this->reportDataObject = $this->client->getReportDataObject($reportID);
    }
    return $this->reportDataObject;
  }
}

?>
like image 204
latca Avatar asked Nov 01 '22 16:11

latca


1 Answers

This problem was fixed in PHP 5.6.1. While I couldn't find any reference to this issue in the change logs, after installing 5.6.1 I can reuse a SoapClient object and do subsequent __doRequest() calls.

PHP Download Link

like image 111
Patkos Csaba Avatar answered Nov 15 '22 04:11

Patkos Csaba