This article mainly introduces php's method of sending xml in the form of post, including curl and fsockopen, which has good reference value, for more information about how php sends xml data in post format, see the following example. Share it with you for your reference. The specific method is as follows:
Method 1: Use curl:
The code is as follows:
$ Xml_data = ... ";
$ Url = 'http: // www.xxxx.com ';
$ Header [] = "Content-type: text/xml"; // Define content-type as xml
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ xml_data );
$ Response = curl_exec ($ ch );
If (curl_errno ($ ch ))
{
Print curl_error ($ ch );
}
Curl_close ($ ch );
Method 2: Use fsockopen:
The code is as follows:
$ Fp = fsockopen ($ server_ip, 80 );
Fputs ($ fp, "POST $ path HTTP/1.0 \ r \ n ");
Fputs ($ fp, "Host: $ server \ r \ n ");
Fputs ($ fp, "Content-Type: text/xml \ r \ n ");
Fputs ($ fp, "Content-Length: $ contentLength \ r \ n ");
Fputs ($ fp, "Connection: close \ r \ n ");
Fputs ($ fp, "\ r \ n"); // all headers sent
Fputs ($ fp, $ xml_data );
$ Result = '';
While (! Feof ($ fp )){
$ Result. = fgets ($ fp, 128 );
}
Return $ result;
I hope this article will help you with PHP programming.