Home >Backend Development >PHP Tutorial >PHP POST XML data via CURL
PHP post xml data to remote address through CURL
1. CURL POST method
$ch = curl_init();
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml; charset=utf-8"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//Post submitted data packet
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
header('Content-Type: text/xml; charset=utf-8');
2. The following is XML data
I keep getting errors after submitting
This page contains the following errors:
error on line 1 at column 50: Space required after the Public Identifier
Below is a rendering of the page up to the first error.
There is no error in querying XML message data information. Please help me solve it. Is it because the CURL method is written incorrectly or is there another problem?
PHP post xml data to remote address through CURL
1. CURL POST method
$ch = curl_init();
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml; charset=utf-8"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//Post submitted data packet
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
header('Content-Type: text/xml; charset=utf-8');
2. The following is XML data
I keep getting errors after submitting
This page contains the following errors:
error on line 1 at column 50: Space required after the Public Identifier
Below is a rendering of the page up to the first error.
There is no error in querying XML message data information. Please help me solve it. Is it because the CURL method is written incorrectly or is there another problem?
<code>$ch = curl_init(); $timeout = 30; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:text/xml; charset=utf-8")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//Post提交的数据包 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_HEADER, 0); $result = curl_exec($ch); curl_close($ch); echo $result;</code>
Sometimes the remote server error returns something like a 500 error. At this time, the returned data is not xml. It is recommended that you print it.
Thank you for the invitation. Got an error after submitting? It is recommended that you make a breakpoint output after submitting to see if there are any error messages. The specific reasons can only be found through bits and pieces of output