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:
- Simultaneous, parallel execution of requests for several remote procedures is a key feature of the library. If the page on your site is made up of 5 remote blocks, each of which is generated by 100ms, you can run them in parallel and get the whole page not for 500ms, but for the same 100ms.
- Reconnect if communication is not possible. Unfortunately, the world is not perfect, and due to accidental packet loss, the first attempt to connect to the SOAP server may end in a timeout. This especially happens when the project is located in several data centers. Dklab_SoapClient allows you to set a timeout for the connection opening time (for example, 1 second) and, in case of failure, try again a specified number of times. In practice, this reduces the likelihood of a final failure by a factor of a thousand, since Reconnect almost always helps with the loss of the package.
- Support timeout for receiving data. If a page is collected from remote blocks, then in the case of “hanging up” of one of them, the whole page also freezes. At the same time, the absence of one of the blocks in the presence of the rest is not such a big trouble. You can specify how long the Dklab_SoapClient should wait for a response from the remote procedure; if the time is exceeded, a PHP exception is thrown, which you can handle at your discretion, without interrupting the loading of the remaining blocks.
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:
- Work with cookies. One procedure can call setcookie (), and the other can read the value of this cooike later.
- Work with PHP sessions. One procedure writes data to the session, the second one reads.
- Support for WSDL schemas and transfer of complex business objects.
- Handling exceptions that occur in a remote procedure.
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.