php通過curl方式實現(xiàn)發(fā)送接收xml數(shù)據(jù)
1、php通過curl方式發(fā)送xml數(shù)據(jù)
<?php
function sendXmlData($url, $xmlData) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// 使用示例
$xmlData = '<root><name>John</name><age>25</age></root>';
$url = 'http://localhost/test22.php';
$response = sendXmlData($url, $xmlData);
// 處理響應
echo $response;發(fā)送XML數(shù)據(jù)的函數(shù)名為sendXmlData,它接受兩個參數(shù):$url是目標服務器的URL,$xmlData是要發(fā)送的XML數(shù)據(jù)。函數(shù)內部使用curl函數(shù)發(fā)送HTTP POST請求,并返回服務器的響應。您可以直接調用sendXmlData函數(shù)來發(fā)送XML數(shù)據(jù)并處理響應結果。
2、php通過file_get_contents接受curl方式發(fā)送xml數(shù)據(jù)
<?php
function receiveXmlData() {
$xmlData = file_get_contents('php://input');
// 解析XML數(shù)據(jù)
$xml = simplexml_load_string($xmlData);
// 處理XML數(shù)據(jù)
// 例如,獲取根元素的值
$name = $xml->name;
$age = $xml->age;
// 返回處理結果
$response = "Received XML Data:\nName: $name\nAge: $age";
return $response;
}
// 使用示例
$response = receiveXmlData();
echo $response;函數(shù)receiveXmlData從輸入流(php://input)中獲取接收到的XML數(shù)據(jù),并使用simplexml_load_string函數(shù)將其解析為可操作的XML對象。您可以根據(jù)需要進一步處理XML數(shù)據(jù),并創(chuàng)建一個包含您要返回的響應的字符串。最后,可以通過調用receiveXmlData函數(shù)并將結果輸出來查看處理結果
結果:

方法補充
除了上文的方法,小編還為大家整理了其他PHP接收發(fā)送XML數(shù)據(jù)的方法,希望對大家有所幫助
使用CURL發(fā)送xml數(shù)據(jù)
<?PHP
$xml_data = '<xml>xml</xml>';//發(fā)送的xml
$url = 'http://localhost/xml/getXML.php';//接收XML地址
$header[] = "Content-type: text/xml";//定義content-type為xml
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//設置鏈接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//設置HTTP頭
curl_setopt($ch, CURLOPT_POST, 1);//設置為POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST數(shù)據(jù)
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //設置超時時間為10
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出錯則顯示錯誤信息
print curl_error($ch);
}
curl_close($ch); //關閉curl鏈接
echo $response;//顯示返回信息接收XML數(shù)據(jù)
<?php
$xml = file_get_contents('php://input');//監(jiān)聽是否有數(shù)據(jù)傳入
if($xml!=''){//如果有數(shù)據(jù)傳入,則將傳入的數(shù)據(jù)寫入到xml.txt文件
$fp = fopen('xml.txt','w');
fwrite($fp,$xml);
fclose($fp);
exit('OK');//寫入成功后返回成功信息
}
die('Fail');//沒有數(shù)據(jù)則直接返回失敗信息php發(fā)送和接收json、xml數(shù)據(jù)
function sendRequest($requestXML)
{
$server = 'http://www.something.com/myapp';
$headers = array(
"Content-type: text/xml"
,"Content-length: ".strlen($requestXML)
,"Connection: close"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
if(curl_errno($ch)){
print curl_error($ch);
echo " something went wrong..... try later";
}else{
curl_close($ch);
}
return $data;
}到此這篇關于php通過curl方式實現(xiàn)發(fā)送接收xml數(shù)據(jù)的文章就介紹到這了,更多相關php發(fā)送接收xml數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PHP實現(xiàn)數(shù)字補零功能的2個函數(shù)介紹
這篇文章主要介紹了PHP實現(xiàn)數(shù)字補零功能的2個函數(shù)介紹,需要的朋友可以參考下2014-05-05
php+ajax實現(xiàn)無刷新數(shù)據(jù)分頁的辦法
這篇文章主要介紹了php+ajax實現(xiàn)無刷新分頁的方法,詳細講述了數(shù)據(jù)庫的創(chuàng)建、Ajax文件的實現(xiàn)及PHP調用方法,需要的朋友可以參考下2015-11-11

