Home >Backend Development >PHP Problem >What should I do if php cannot receive xml data?

What should I do if php cannot receive xml data?

藏色散人
藏色散人Original
2022-10-24 11:19:101264browse

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.

What should I do if php cannot 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!

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