PHP Async Web Services

How to make an asynchronous call to a web service using the PHP SOAP Extension ?

+4
source share
9 answers

My immediate answer should be: you cannot.
PHP does not have the streaming capabilities that can be used in userland.

Now, if you really want to do this, there are several ways around this:

  • Use the exec functions to start another process in the background and control it through a database / file system or something else.
  • Use the fork function to start another process and control it through a database / file system or something else.

The elimination of these two approaches is that you can make it asynchronous, but if you want a callback, then it will be very difficult and not at all trivial. Well, it wonโ€™t even be a callback, since you wonโ€™t be able to wait for it in a script, which makes an asynchronous call. This means that you can only have some kind of monitoring scheme. I would suggest AJAX.

+4
source

If you use curl, it has a set of โ€œmultiโ€ calls to allow concurrent calls to multiple servers ...

+1
source

It can be help (calls to a parallel remote procedure): http://en.dklab.ru/lib/Dklab_SoapClient/

+1
source

You will need to write a SoapServer class that will continue processing after disconnecting the client. This article will give you a starting point, but you will have to wrap something like this inside the SoapServer class.

It will look something like this (note: I have not tested this inside SoapServer, but it gives you an idea)

 class NonBlockingSoapServer extends SoapServer { public function handle() { // this script can run forever set_time_limit(0); // tell the client the request has finished processing header('Location: index.php'); // redirect (optional) header('Status: 200'); // status code header('Connection: close'); // disconnect // clear ob stack @ob_end_clean(); // continue processing once client disconnects ignore_user_abort(); ob_start(); /* ------------------------------------------*/ /* this is where regular request code goes.. */ $result = parent::handle(); /* end where regular request code runs.. */ /* ------------------------------------------*/ $iSize = ob_get_length(); header("Content-Length: $iSize"); // if the session needs to be closed, persist it // before closing the connection to avoid race // conditions in the case of a redirect above session_write_close(); // send the response payload to the client @ob_end_flush(); flush(); /* ------------------------------------------*/ /* code here runs after the client diconnect */ /* YOUR ASYNC CODE HERE ...... */ return $result; } } 
+1
source

One way is to use the select() ing method provided by the "multi" SoapClient package by extending the SoapClient class and implementing your own __doRequest .

The smallest working example that I found can be downloaded https://github.com/halonsecurity/sp-enduser/blob/master/inc/soap.php and is used as

 $client1 = new SoapClientAsync('some-systems-wsdl', $options); $client2 = new SoapClientAsync('another-systems-wsdl', $options); $client1->someFunction($arguments); $client2->anotherFunction($arguments); soap_dispatch(); $result1 = $client1->someFunction($arguments); $result2 = $client1->anotherFunction($arguments); 

as described here http://www.halon.se/blogs/making-phps-soap-client-asynchronous/

+1
source

If you have the ability to make a php command line call on Linux, you can run the pnctl_fork command and call the web service from the forked child process.

0
source

Make it client, not server, with an AJAX call.

0
source

Try the method they gave me in my question: Asynchronous PHP calls?

0
source

I do not know why Gustavo was changed, since his correct answer.

I use exec to run a shell script written in PHP that communicates with the google API. I run the script as follows:

run.php param1 = 1 param2 = 2 &> ajax.txt

last line of run

echo 'finished'

then my ajax continues to poll 'ajax.txt' until it finds that the process is complete.

Hacky But Simple (KISS)

monk.e.boy

0
source

Source: https://habr.com/ru/post/1277120/


All Articles