📜 ⬆️ ⬇️

SoapClient: parallel asynchronous requests, reconnect, timeout handling

Dklab_SoapClient is an advanced version of the standard PHP class SoapClient, designed for parallel (asynchronous) remote procedure calls in high-load projects.

With the help of this library you can, for example, build a page of your site from blocks, like from a constructor. Each block is requested through SOAP separately and independently from the others, with all requests occurring in parallel. If one of the blogs did not meet the allotted time (timeout), then it can not be displayed on the page.

Compared with the built-in PHP SoapClient, additional features are supported:Why SOAP? At first glance, it may seem like a very cumbersome protocol that is difficult to apply. However, if you do not delve into the internals of the protocol, it turns out that it is very convenient to use in PHP SOAP. If WSDL schemes are not needed, the simplest SOAP server is written as:
')
 class MyServer
 {
     public function getComplexData ($ some)
     {
         sleep (1);  // type, we emulate heavy code
         return array ("obj" => (object) array ("prop" => $ some), "some" => "thing");
     }
 }
 $ soapServer = new SoapServer (null, array ('uri' => 'urn: myschema'));
 $ soapServer-> setObject (new MyServer ());
 $ soapServer-> handle ();

And the corresponding SOAP client is like this:

 require_once "Dklab / SoapClient.php";
 $ client = new Dklab_SoapClient (null, array (
     'location' => "http://example.com/server.php",
     'uri' => 'urn: myschema',
     'timeout' => 3,
 ))
 // Requests are executed in parallel, asynchronously.  In total - for 1 s.
 $ query1 = $ client-> async-> getComplexData (array ("abc"));
 $ query2 = $ client-> async-> getComplexData (array ("xyz"));
 print_r ($ query1-> getResult ());
 print_r ($ query2-> getResult ());

What is important, when working with SOAP in PHP, the server can return data in an arbitrary format (for example, an array of arrays of objects), and the client can transfer variables of any structure to the server in parameters (for example, the same array of arrays of objects). In this case, all conversions are performed automatically.

Because Dklab_SoapClient is an extension of the embedded SoapClient, it supports all the standard features of the latter, including:If suddenly Apache Thrift for your project - “from a cannon on a sparrow”, Dklab_SoapClient can serve as a simple and compact alternative. (By the way, parallel queries are not supported in the PHP module for Thrift - at least I did not find them there.)

View documentation, examples, autotests and download the library here: http://dklab.ru/lib/Dklab_SoapClient/

PS
By the way, Git and the GitHub social network are used in development. Who else did not try them, I recommend it.

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


All Articles