Php sends xml in the form of post, and xml in the form of phppost

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.