David Lences
David Lences

Reputation: 113

PHP - How to read data from SOAP XML

I have code for read XML SOAP 1.2 from web service. Im using this: https://stackoverflow.com/a/18580428/2629513

I get this code below:

 SimpleXMLElement Object
(
[OdkazyResponse] => SimpleXMLElement Object
    (
        [OdkazyResult] => SimpleXMLElement Object
            (
                [odkazy] => SimpleXMLElement Object
                    (
                        [odkaz] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [kod_zbozi] => 31400001
                                                [typ] => OBR1
                                                [popis] => Oki ML 280 - foto
                                                [url] => http://www.atcomp.cz/katalog/31400001/ML280.gif
                                            )

                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [kod_zbozi] => EC376123GB
                                                [typ] => OBR1
                                                [popis] => Malý obrázek
                                                [url] => http://www.atcomp.cz/katalog/EC376123GB/lq-680_-_maly.jpg
                                            )

                                    )

                                [2] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [kod_zbozi] => EC376123GB
                                                [typ] => OBR2
                                                [popis] => Velký obrázek
                                                [url] => http://www.atcomp.cz/katalog/EC376123GB/lq-680_-_velky.jpg
                                            )

                                    )

And how I can read the [kod_zbozi], [typ], [popis], [url] attributes? I need to save it into my mysql database (this is not problem, the problem is read the data from this format XML). Thanks.

Upvotes: 0

Views: 478

Answers (1)

Yang
Yang

Reputation: 8711

Well, the very first thing you might want to do is to convert that object into an array (to avoid naming problems)

You can use this function to do that:

  function object2array($object) { 
    return json_decode(json_encode($object), true); 
  }

then something like this:

$data = object2array(simplexml_load_string('....'));

print_r($data); // Its regular array now, use it keys to access values, then simply insert them into db

Upvotes: 1

Related Questions