Serializing a SOAPClient PHP Object

I am writing a PHP application that uses several SOAP web services to collect data.

I get significant overhead when creating all these objects: in some cases, one line of code $object = new SoapClient($wsdl); may take more than three seconds. Obviously, only a few of them make the web page very slow.

To speed things up a bit, I decided that I would serialize the objects and save them in the session (or somewhere similar), so I wrote the following function:

 function soap_client($name,$wsdl) { if (!isset($_SESSION['soapobjects'][$name])) { $client = new SoapClient($wsdl, array('trace' => 1)); $_SESSION['soapobjects'][$name]=serialize($client); } else { $client = unserialize($_SESSION['soapobjects'][$name]); } return $client; } 

This certainly looks like the way PHP recommends .

... and then calling it that ...

 $client = soap_client('servicename',$wsdl); $client->MethodName($parameters); 

However, it does not work.

At the first start, it works (i.e. an object is created and a serialized copy is created, and the method call works fine). However, the second time you run it, it fails.

The object looks serialized and deserizable correctly, but when you try to make a SOAP call on a de-serialized object, it throws the following error:

 Fatal error: Uncaught SoapFault exception: [Client] Error finding "uri" property 

Obviously, the de-serialized object does not match the original object, which contradicts the way that serializing objects is supposed to work.

Can someone explain why I am getting this error? Can you suggest a way to make it work or an alternative strategy that I could convince?

Thanks.

ps - I tried to solve the problem, but not joy.

I tried to specify the URI in the parameter parameter (as indicated in the PHP SOAP Client manual ), but it did not make any difference. But in any case, this is not necessary, since I use WSDL.

I also tried just copying the object to $_SESSION without using serialize() and deserialize() , but it has exactly the same effect.

+4
source share
2 answers

The native SOAP extension is illegible binary horror code. It is very likely that it was not built with serialization. For example, it may contain an internal file descriptor that the serialize / unserialize process cannot handle. I highly recommend you use a different SOAP client, for example:

  • Zend_Soap , part of the Zend Framework. You do not need to use the Framework in any other area of ​​your code, and possibly remove most of the other components. However, it seems to be using the existing SOAP extension under the covers, so it probably isn't a good candidate for serialization.
  • PEAR SOAP is often referenced, although a bit outdated.
  • NuSOAP was recently returned from the dead, although all of the online documentation seems to have disappeared into zip files.

If none of these work, consider caching the WSDL file locally, as I somehow expect that where there is a delay.

+3
source

According to your comments on the previous answer, the best option would be to use the Zend framework only for soap, as suggested earlier, this will allow you to continue to use php and still have better functionality with zend. You can also use other Zend features to increase your requirements.

This example can help you http://blog.fedecarg.com/2009/02/15/building-a-web-service-client-using-the-zend-framework/

+2
source

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


All Articles