Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Is it better to close SoapClient connection?

I created the following helper function:

function mainSoap(){
    return new SoapClient('https://soap.url',[
        'stream_context' => stream_context_create([
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            ]
        ]);
    ]);
}

and I'm using it like $result=mainSoap()->GetSometing($parameters);

Everything works fine, but in terms of performance, I would like to know if is better to close the soap connection or not.

UPDATE

I created another function in order to close the connection after getting the response, but I still could't find a close method for SoapClient.

function mainSoap(string $call=null, array $vars=[]){
    $url="https://soap.url";
    $wsdl=new SoapClient($url,[
        'trace' => false,
        'keep_alive' => false,
        'stream_context' => stream_context_create([
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            ]
        ]),
        'compression'   => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE
    ]);
    $response=$wsdl->__soapCall($call,$vars);
    //Looking for something like $wsdl->close();
    return $response;
}

and I'm using it like $result=mainSoap('GetSometing',[$parameters]);

like image 473
Vixed Avatar asked Sep 06 '18 09:09

Vixed


1 Answers

The SoapClient follows the normal HTTP Request life cycle,

That is to say, your SoapClient does not maintain a constant connection to the server, it will only connect out and receive data when called, similarly to how your browser does not maintain a connection to a server once a request completes, as the request is terminated, unless you set keep_alive to true.

So, in short, you can't "close" a SoapClient because there's nothing to close after you've called it, it's already closed the connection.

Relevant Links:

  • Wikipedia article on Http (Persistent Connections Section)
  • MDN Documentation on Keep-Alive header
like image 152
Zachary Craig Avatar answered Oct 21 '22 07:10

Zachary Craig