Php sends xml in the form of post, and xml in the form of phppost
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 = <xml>... </xml> ";
$ 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.
Php transfers xml via post. I am a new php user. I hope I can write more details.
? What do you mean
How does php post XML to a specified server?
===================================== Supplementary answer
The following is a written post XML class:
<? Php
Class xmlSender {
/**
* Constructor
* Check whether cURL is available
*/
Function xmlSender ()
{
If (! Extension_loaded ('curl ')){
Trigger_error ("You need cURL loaded to use this class", E_USER_ERROR );
}
}
/**
* The cURL library is used to send xml content
*/
Function send ($ str_xml, $ str_url, $ str_page, $ boo_ssl = false)
{
$ Str_header = "POST". $ str_page. "HTTP/1.0 \ r \ n ";
$ Str_header. = "MIME-Version: 1.0 \ r \ n ";
$ Str_header. = "Content-type: application/PTI26 \ r \ n ";
$ Str_header. = "Content-length:". strlen ($ str_xml). "\ r \ n ";
$ Str_header. = "Content-transfer-encoding: text \ r \ n ";
$ Str_header. = "Request-number: 1 \ r \ n ";
$ Str_header. = "Document-type: Response \ r \ n ";
$ Str_header. = "Interface-Version: Site 1.0 \ r \ n ";
$ Str_header. = "Connection: close \ r \ n ";
$ Str_header. = $ str_xml;
$ Res_curl = curl_init ();
Curl_setopt ($ res_curl, CURLOPT_URL, $ str_url );
Curl_setopt ($ res_curl, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ res_curl, CURLOPT_TIMEOUT, 30 );
Curl_setopt ($ res_curl, CURLOPT_CUSTOMREQUEST, $ str_header );
Curl_setopt ($ res_curl, CURLOPT_FOLLOWLOCATION, 1 );
... The remaining full text>