How to convert php interface xml to array

PHPz
Release: 2023-04-26 09:31:48
Original
581 people have browsed it

With the development of the Internet, various interfaces are becoming more and more important. Whether it is data exchange between enterprises or data integration, interfaces need to be called. In the process of calling the interface in PHP, it is often necessary to convert the XML returned by the interface into an array. Therefore, this article will explain how to call an interface in PHP and convert XML to an array.

1. Method of calling the interface

In PHP, the most common way to call the interface is to use the CURL library. You can use the CURL library to easily perform HTTP requests, including GET, POST, PUT, DELETE and other request methods. Suppose we need to call an interface that returns data in XML format. The calling process is as follows:

  1. Initialize CURL

$curl = curl_init();

  1. Set the request address and request method

curl_setopt($curl, CURLOPT_URL, "http://example.com/api");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);

  1. Set the parameters of the POST request

$post_data = array(

'key' => 'value',
'key2' => 'value2'
Copy after login

);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

  1. Execute the request and get the response

$response = curl_exec($curl);

  1. Close CURL

curl_close($curl);

In the above code, $response is the XML format data returned by the interface, and then it needs to be converted to array.

2. XML to Array Conversion Method

We can use PHP's built-in SimpleXML library to convert XML format data into objects or arrays. The usage method is as follows:

  1. Convert XML format data into objects

$xml = simplexml_load_string($response);
print_r($xml);

  1. Convert XML format data to an array

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

In the above code, first use the simplexml_load_string function to convert the XML format data into an object, then use json_encode to convert the object into JSON format data, and then use Use json_decode to convert JSON format data into an array, thus achieving XML to array conversion.

3. Avoid XML format issues

In the above method, if the XML format returned by the interface is not standardized or has problems, the simplexml_load_string function conversion will fail. To avoid this situation, you can use PHP's built-in DomDocument library to parse XML format data. The usage is as follows:

  1. Initialize DomDocument

$doc = new DOMDocument();

  1. Load XML data

$doc->loadXML($response);

  1. Get XML elements and convert to array

$result = array();
foreach ($doc->getElementsByTagName("*") as $node) {

//如果元素没有子元素,就获取元素值
if (!$node->hasChildNodes()) {
    $result[$node->nodeName] = $node->nodeValue;
} else {

    //否则,递归获取子元素
    foreach ($node->childNodes as $childNode) {
        if ($childNode->nodeType != XML_TEXT_NODE) {
            $result[$node->nodeName][] = array(
                $childNode->nodeName => $childNode->nodeValue
            );
        }
    }
}
Copy after login

}

print_r($result);

In the above code, we created A DOMDocument object and uses the loadXML function to load XML format data into the DOM. Next, we use the getElementsByTagName function to obtain XML elements and obtain subelements recursively. In this way, conversion failures due to XML format issues can be avoided.

4. Summary

This article introduces the method of calling the interface in PHP and converting XML format data into an array. By using the CURL library, you can easily make HTTP requests; by using the SimpleXML or DomDocument library, you can convert XML format data into an array or object. Using the DomDocument library can better avoid conversion failures caused by XML format issues. We hope this article can provide some help to the problems encountered by PHP developers during the interface calling process.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template