Home >Backend Development >PHP Tutorial >How to send xml in php in post form, phppost form xml_PHP tutorial

How to send xml in php in post form, phppost form xml_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:121090browse

php method to send xml in post format, phppost format xml

The example in this article describes the method of sending XML in PHP form. Share it with everyone for your reference. The specific method is as follows:

Method 1, use curl:

Copy code 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:

Copy code The code is as follows:
$fp = fsockopen($server_ip, 80);
fputs($fp, "POST $path HTTP/1.0rn");
fputs($fp, "Host: $serverrn");
fputs($fp, "Content-Type: text/xmlrn");
fputs($fp, "Content-Length: $contentLengthrn");
fputs($fp, "Connection: closern");
fputs($fp, "rn"); // all headers sent
fputs($fp, $xml_data);
$result = '';
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
return $result;

I hope this article will be helpful to everyone’s PHP programming design.

php sends xml through post. I am new to php, I hope I can write more details

? What does it mean

How to post XML to the designated server in php

============================== Supplementary answer
The following is a written post XML class:


class xmlSender {

/**
* Constructor
* Verify whether cURL is available
*/
function xmlSender()
{
if ( !extension_loaded('curl') ) {
trigger_error("You need cURL loaded to use this class", E_USER_ERROR);
}
}

/**
* Use cURL library 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\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 rest of the full text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906111.htmlTechArticleHow to send xml in php post format, phppost format xml This example describes the method of php sending xml in post format . Share it with everyone for your reference. The specific methods are as follows: Method 1, use...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn