PHP Soap Client is Very Slow on First Connection

I use php SoapClient to call M3 (Movex) webservices, stored on our internal server. My Symfony 3.4 project which does the calls is on another internal linux server.

I noticed that the initialization of the soap client is very slow (2-3 minutes) when it’s done after a long time without using it (several hours). For example, it occurs the morning, when I test my project for the first time of the day. But I have no problem for every initializations and calls done after several minutes (responds in 500ms).

Because of the 1st init takes several minutes, my nginx server returns a 504 gateway time-out error after 1 minute.

The webservices urls are in HTTPS and we use SSL certificates. We also had to authenticate with a login and a password. I use a session cookie, available for 8 hours, that I add in the HTTP request header. We don’t use a proxy.

I think it’s a cache problem. When I’m using SoapUI, I don’t have the problem, the webservice responds very fastly.

Here is the configuration of the soap extension in my php.ini :

enter image description here

Here is my soap client init :

$client = new SoapClient("https://my-domain.com:55080/my-webservice?wsdl", array(
  'login'     => $login,
  'password'  => $pwd,
  'trace'     => true,
  'exceptions' => true,
  'stream_context' => stream_context_create(array(
    'http' => ['header' => 'cookie: ' . $cookie]
  ))
));

I tried to add 'cache_wsdl' => WSDL_CACHE_MEMORY in the options but it’s worse, every time I’m using it, it’s very slow.

Here are the logs to see where the problem occurs (look at lines 3 and 4 -> 2 minutes) :

01/10/19 09:58:06 ------- Get Customer -------
01/10/19 09:58:06 - Cookie exists -> send it in the request
01/10/19 09:58:06 - BEGIN Init soap client
01/10/19 10:00:07 - END Init soap client
01/10/19 10:00:07 - BEGIN client->GetCustomerData
01/10/19 10:00:07 - END client->GetCustomerData
01/10/19 10:00:07 ------- END Get Customer -------

When I test it again after several minutes (no problem, done in the same second) :

01/10/19 10:03:52 ------- Get Customer -------
01/10/19 10:03:52 - Cookie exists -> send it in the request
01/10/19 10:03:52 - BEGIN Init soap client
01/10/19 10:03:52 - END Init soap client
01/10/19 10:03:52 - BEGIN client->GetCustomerData
01/10/19 10:03:52 - END client->GetCustomerData
01/10/19 10:03:52 ------- END Get Customer -------

This Post Has One Comment

  1. No Fault

    As an answer to your question in your comments you can try to disguise your soap client as a browser. This might be a possible solution. There ‘s no garanty, that this is the one and only solution. 😉

    // options
    $wsdl = ‘https://your.service.url.here?wsdl’;

    $options = [
    ‘trace’ => true,
    ‘exceptions’ => true,
    ‘cache_wsdl’ => WSDL_CACHE_NONE,
    ‘connection_timeout’ => 30,
    ‘user_agent’ => ‘Mozilla/1.0N (Windows)’,
    ];

    try {
    $client = new SoapClient($wsdl, $options);
    // more logic here
    } catch (SoapFault $e) {
    var_dump($e);
    }

Leave a Reply