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:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://example.com/api");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
$post_data = array(
'key' => 'value', 'key2' => 'value2'
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($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:
$xml = simplexml_load_string($response);
print_r($xml);
$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:
$doc = new DOMDocument();
$doc->loadXML($response);
$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 ); } } }
}
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!