Home  >  Article  >  Backend Development  >  How to convert xml returned by soap into an array in php

How to convert xml returned by soap into an array in php

PHPz
PHPzOriginal
2023-03-31 09:09:53904browse

When using web services, when we use SOAP as the transmission protocol, we will receive a return result in XML format. Sometimes, we need to convert this XML data into an array so that we can use this data more conveniently. In PHP, this conversion is very simple and we can do it using PHP's built-in functions.

1. XML format example of SOAP return value

When using web services, we generally send requests through the SOAP protocol and get a return result in XML format. The returned results in XML format can be complex, but we can understand it with some examples. The following is a simple XML format example of a SOAP return value:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/webservice.php">
  <SOAP-ENV:Body>
    <ns1:response>
      <ns1:result>
        <name>John Smith</name>
        <age>30</age>
        <country>USA</country>
      </ns1:result>
      <ns1:result>
        <name>Alice Jones</name>
        <age>25</age>
        <country>UK</country>
      </ns1:result>
    </ns1:response>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The above XML code contains an example of a SOAP return value. Its root node is <SOAP-ENV:Envelope>, which contains a <SOAP-ENV:Body> node, <SOAP-ENV:Body&gt ;There is a <ns1:response> node in the node, and there are two <ns1:result> nodes in this node. Each <ns1:result> node contains three element nodes: <name>, <age> and <country&gt ;.

This XML return value contains the information of two people. We can convert it into an array through PHP to facilitate us to process this data.

2. Use PHP's simplexml_load_string() function to convert XML into an array

In PHP, we can use the built-in simplexml_load_string() function to convert XML into an array. In the above XML example, we can convert it to an array using the following PHP code:

$xmlstr = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/webservice.php">
  <SOAP-ENV:Body>
    <ns1:response>
      <ns1:result>
        <name>John Smith</name>
        <age>30</age>
        <country>USA</country>
      </ns1:result>
      <ns1:result>
        <name>Alice Jones</name>
        <age>25</age>
        <country>UK</country>
      </ns1:result>
    </ns1:response>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>';

$xml = simplexml_load_string($xmlstr);
$json = json_encode($xml);
$array = json_decode($json, true);

print_r($array);

In the above code, we first copy the XML string into the $xmlstr variable , and then use the simplexml_load_string() function to convert it to a PHP object. Next, we use the json_encode() and json_decode() functions to convert the PHP object into an array, and finally use the print_r() function to print out the array.

Run this code, we will get the following output:

Array
(
    [response] => Array
        (
            [result] => Array
                (
                    [0] => Array
                        (
                            [name] => John Smith
                            [age] => 30
                            [country] => USA
                        )
                    [1] => Array
                        (
                            [name] => Alice Jones
                            [age] => 25
                            [country] => UK
                        )
                )
        )
)

The above output shows a PHP array, which contains the XML transformed data returned by SOAP. We can use this data very conveniently for processing.

3. Use PHP's SimpleXMLElement class

In addition to the simplexml_load_string() function mentioned above, we can also use PHP's built-in SimpleXMLElement class to convert XML into an array.

In the following code, we use PHP's built-in file_get_contents() function to obtain XML data from the specified URL:

$xmlstr = file_get_contents('http://localhost/webservice.php');

$xml = new SimpleXMLElement($xmlstr);
$json = json_encode($xml);
$array = json_decode($json, true);

print_r($array);

In the above code, we first use file_get_contents( ) function obtains XML data, and then uses PHP's built-in SimpleXMLElement class to convert the XML into a PHP object. Next, we use the json_encode() and json_decode() functions to convert the PHP object into an array, and finally use the print_r() function to print out the result.

The above code is very similar to the previous example, the only difference is that we use the SimpleXMLElement class to convert XML into objects. After conversion, we can use this data like an array.

Summary

This article introduces how to use PHP's built-in functions to convert the XML data returned by SOAP into an array. These techniques can make it easier for us to use the return value of the web service, thereby speeding up our development and integration. Whether using the simplexml_load_string() function or the SimpleXMLElement class, we can easily convert XML into a PHP array.

The above is the detailed content of How to convert xml returned by soap into an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn