Hello guys,
Do you use PHP? Do you use SOAP (you better :p) default extension?
This post is to be applied in that case and in that case only.
I don’t like NuSOAP and I think it has no use anymore.
This is a pretty neat ‘hack’. After sending some data to a webservice, say you need to save the XML that is easy.. PHP gives you your request in XML.
But say you need to save the payload as it is, just to re-send it later for some reason.
The payload is the final PHP Soap Object you constructed. (Works in arrays too as the PHP SOAP Object needs to be converted to array to be sent anyway).
You may say, well I will use serialize, and store the serialization of the object, however PHP serialize does NOT work with PHP SOAP Objects.
It doesn’t recognize the classes and the objects get malformed.
This is a trouble you only experience if you are really trying to rebuild a SOAP Object and not a simple array, but trust me there are cases in which you need SOAP Objects and not just arrays to enforce complicated hierarchy of namespaces.
You move on to json_encode/decode? Then it just won’t work as all the objects will be re-constructed into Array’s..
So what you actually need is a re-constructive function that applies the correct decode transformation to a previous json_encode of the entire Payload SOAP Object.
So after you apply json_decode and you have a extensive Array. You apply this “””beautiful””” (read neat hack) function I created to reconstruct it accordingly.
You may want to add some switch’s in case you used any different type besides XSD_STRING or just SOAP_ENC_OBJECT when constructing the parameters.
I don’t use any other type seeing as it doesn’t matter when it gets transformed to XML.
Here’s the neat function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
function soapifyAO(array $data, $type_cast) { foreach ($data as $k=> &$value) { if (is_array($value)) { if ($k == '0') $value = soapifyAO($value, 2); elseif ($k == 'enc_value') $value = soapifyAO($value, 3); elseif (isset($value[0])) $value = soapifyAO($value, 1); else $value = soapifyAO($value, 2); } } switch ($type_cast) { case 1: return new ArrayObject($data); break; case 2: switch ($data['enc_type']) { case 101: return new SoapVar($data['enc_value'], XSD_STRING, NULL, $data['enc_ns'], $data['enc_name'], $data['enc_namens']); break; case 301: return new SoapVar($data['enc_value'], SOAP_ENC_OBJECT, NULL, $data['enc_ns'], $data['enc_name'], $data['enc_namens']); break; } break; case 3: return (object) $data; break; } } |
Enjoy. Share!
Comments to "Reconstruct PHP SOAP Object"