{"id":5684,"date":"2014-04-05T04:22:23","date_gmt":"2014-04-05T04:22:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/05\/serializing-a-php-soapclient-object-collection-of-common-programming-errors\/"},"modified":"2014-04-05T04:22:23","modified_gmt":"2014-04-05T04:22:23","slug":"serializing-a-php-soapclient-object-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/05\/serializing-a-php-soapclient-object-collection-of-common-programming-errors\/","title":{"rendered":"Serializing a PHP SOAPClient object-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m writing a PHP application which uses a number of SOAP web services to gather data.<\/p>\n<p>I&#8217;m getting significant overheads in instantiating all those objects: in some cases a single line of code <code>$object = new SoapClient($wsdl);<\/code> can take over three seconds. It obviously only takes a few of those to make a web page feel really slow.<\/p>\n<p>To speed things up a bit, I figured I&#8217;d serialise the objects and store them in the session (or somewhere similar), so I wrote the following function:<\/p>\n<pre><code>function soap_client($name,$wsdl) {\n    if (!isset($_SESSION['soapobjects'][$name])) {\n        $client = new SoapClient($wsdl, array('trace' =&gt; 1));\n            $_SESSION['soapobjects'][$name]=serialize($client);\n        } else {\n            $client = unserialize($_SESSION['soapobjects'][$name]);\n        }\n    return $client;\n}\n<\/code><\/pre>\n<p>That certainly seems to be the way PHP recommends to do it.<\/p>\n<p>&#8230;and then calling it like this&#8230;<\/p>\n<pre><code>$client = soap_client('servicename',$wsdl);\n$client-&gt;MethodName($parameters);\n<\/code><\/pre>\n<p>However, it doesn&#8217;t seem to work.<\/p>\n<p>The first time you run it, it works (ie the object is created and a serialised copy is made, and the method call works fine). However the second time you run it, it fails.<\/p>\n<p>The object <em>appears<\/em> to serialise and de-serialise correctly, but when you try to execute a SOAP call on the de-serialised object, it throws the following error:<\/p>\n<pre><code>Fatal error: Uncaught SoapFault exception: [Client] Error finding \"uri\" property\n<\/code><\/pre>\n<p>Clearly the de-serialised object is not the same as the original object, which is at odds with how object serialisation is supposed to work.<\/p>\n<p>Can anyone explain why I&#8217;m getting this error? Can you suggest a way to get it working, or an alternative strategy that I could be persuing?<\/p>\n<p>Thank you.<\/p>\n<p>ps &#8211; I&#8217;ve tried to work around the problem, but no joy.<\/p>\n<p>I&#8217;ve tried specifying the URI in the options parameter (as specified in the PHP SOAP Client manual), but it didn&#8217;t make any difference. But it shouldn&#8217;t be necessary anyway, as I&#8217;m using a WSDL.<\/p>\n<p>I&#8217;ve also tried simply copying the object to <code>$_SESSION<\/code>, without using <code>serialize()<\/code> and <code>deserialize()<\/code>, but that has exactly the same effect.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m writing a PHP application which uses a number of SOAP web services to gather data. I&#8217;m getting significant overheads in instantiating all those objects: in some cases a single line of code $object = new SoapClient($wsdl); can take over three seconds. It obviously only takes a few of those to make a web page [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5684","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5684"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5684\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}