Home > Article > Backend Development > What should I do if php cannot receive xml data?
Solution to the problem that php cannot receive xml data: 1. Use the "file_get_contents('php://input');" method to receive xml data; 2. Use "simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'] , 'SimpleXMLElement', LIBXML_NOCDATA);" method to receive xml data.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
What should I do if php cannot receive xml data?
About some issues with php accepting xml, simplexml_load_string cannot receive data
These two methods are generally used to accept xml data
Method 1:
file_get_contents('php://input');
Method 2:
simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);
It is recommended to use method 1, method 2 has been discarded in higher PHP versions
----------------- -------Good-looking dividing line------------------
Foreword: The company's projects are somewhat old, and they all use method 2.
Problem: When I use curl to request an interface written by others, method 2 cannot receive it, but method 1 can.
Solution: After testing, the Content-Type in the cur parameter that needs to be set is text/xml
Code: curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/ xml; charset=utf-8"));
The default value of Content-Type in PHP's cur parameter is application/x-www-form-urlencoded, tmd! Using method 2 will determine the Content-Type of the http request header
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What should I do if php cannot receive xml data?. For more information, please follow other related articles on the PHP Chinese website!