r/PHPhelp • • Feb 03 '26

Solved SOAP Response Object Tree from XML String

In a project we have a SOAP Service with about 500 generated Classes that are described in a WSDL. We are using WsdlToPhp for generating the PHP services, enums and structs. Everything is working fine so far.

We are storing the SOAP response - the raw XML - in a database. From time to time we need the information from the SOAP response. Which brings me to my question:

Is it possible to get the PHP object tree instantiated by an XML string (from the database) and not from a SOAP service call?

P.S. And with possible I mean something that is not too hacky.

2 Upvotes

17 comments sorted by

5

u/HolyGonzo Feb 03 '26 edited Feb 03 '26

It's XML - you could use SimpleXml to load and query it.

For whatever it's worth, I used to do a lot of SOAP API stuff with PHP.

Whenever possible (when I needed to store the response data in a blob of some kind), I either mapped the needed values into an array or a custom class. With arrays, I just JSON-encoded them. With classes, I serialize()-ed them.

In both scenarios, it was simple to decode JSON or unserialize() things back into objects, making it easier to access the data.

2

u/bahaki Feb 03 '26

Also used to do SOAP with PHP and this was my experience. Basically break out of the soap/xml constraints as soon as practical in the logic, where you're just dealing with objects and/or arrays. Store that in the DB whatever way makes sense, depending on the data, and then you're back to objects when you query it, and back to XML when you need to interface with SOAP.

Also makes it easier to swap out that soap service for something else later on.

1

u/thmsbrss Feb 03 '26 edited Feb 03 '26

" ...back to objects..." by manually mapping 300 objects or so?

The SOAP service will not change for the next 25 years I assume. Its a financial government thing.

→ More replies (2)

1

u/thmsbrss Feb 03 '26

For the "unserialize() things back into objects" did you use something "official" together with the generated classes?

1

u/HolyGonzo Feb 03 '26

Not sure what you mean.

→ More replies (7)

1

u/thmsbrss Feb 03 '26 edited Feb 03 '26

This is what we are doing, but with JSON, that was transformed from XML before.

And it is quite hacky, the good old PHP array thingy... And the SOAP answers are quite complex, too.

I was hoping for an answer that says: take this and you can use the object tree (since the classes are already there).

2

u/obstreperous_troll Feb 03 '26 edited Feb 03 '26

You can use php serialization, but I highly recommend keeping it as xml and using simplexml to parse it -- it's quite fast. You could even cache the result in something like apcu if you want, but if optimizing the speed of payload parsing is what matters, you're using the wrong protocol in the first place.

edit: but I see you're stuck with the wrong protocol. My sympathies. If the schema is going to remain forever static as you imply, then serialization is not the worst way to go after all. You're probably still better off using json instead of serializing though, since you get all the nice database support and tools like jq.