4

After a user signs up on my website i need to send a soap request in a method that is not blocking to the user. If the soap server is running slow I don't want the end user to have to wait on it. Is there a way I can send the request and let my main PHP application continue to run without waiting from a response from the soap server? If not, is there a way to set a max timeout on the soap request, and handle functionality if the request is greater than a max timeout?

Edit: I would ideally like to handle this with a max timeout for the request. I have the following:

//ini_set('default_socket_timeout', 1);
      $streamOptions = array(
              'http'=>array(
                'timeout'=>0.01
            )
      );

      $streamContext = stream_context_create($streamOptions);

      $wsdl   = 'file://' . dirname(__FILE__) . '/Service.wsdl';

        try{
          if ( file_get_contents( $wsdl ) ) {

              $this->_soapClient = new SoapClient($wsdl,
                  array(
                      'soap_version' => SOAP_1_2,
                      'trace' => true,
                      'stream_context' => $streamContext
                  )
              );
              $auth = array('UserName' => $this->_username, 'Password' => $this->_password);
              $header = new SoapHeader(self::WEB_SERVICE_URL, "WSUser", $auth);
              $this->_soapClient->__setSoapHeaders(array($header));

          }//if
      }
      catch(Exception $e){
        echo "we couldnt connect". $e;
      }
$this->_soapClient->GetUser();

I set the timeout to 0.01 to try and force the connection to timeout, but the request still seems to fire off. What am I doing wrong here?

Dan Ramos
  • 1,092
  • 2
  • 19
  • 35

2 Answers2

2

I have had the same issues and have implemented solution !

I have implemented

SoapClient::__doRequest();

To allow multiple soap calls using

curl_multi_exec();

Have a look at this asynchronous-soap

silverdr
  • 1,978
  • 2
  • 22
  • 27
Meabed
  • 3,828
  • 1
  • 27
  • 37
1

Four solutions:

  1. Use AJAX to do the SOAP -> Simplest SOAP example

  2. Use AJAX to call a second PHP file on your server which does the SOAP (best solution imo)

  3. Put the SOAP request to the end of your PHP file(s) (not the deluxe solution)

  4. Use pcntl_fork() and do everything in a second process (I deprecate that, it might not work with every server configuration)

Depending on the way you implement this, PHP has plenty of timeout configurations, for example socket_set_timeout(), or stream_set_timeout() (http://php.net/manual/en/function.stream-set-timeout.php)

Community
  • 1
  • 1
Daniel W.
  • 31,164
  • 13
  • 93
  • 151