Php sends xml in the form of post
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, see
This example describes how php sends xml in the form of post. Share it with you for your reference. The specific method is as follows:
Method 1: Use curl:
Copy codeThe 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:
Copy codeThe 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.