php通過curl方式實(shí)現(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);
// 處理響應(yīng)
echo $response;發(fā)送XML數(shù)據(jù)的函數(shù)名為sendXmlData,它接受兩個參數(shù):$url是目標(biāo)服務(wù)器的URL,$xmlData是要發(fā)送的XML數(shù)據(jù)。函數(shù)內(nèi)部使用curl函數(shù)發(fā)送HTTP POST請求,并返回服務(wù)器的響應(yīng)。您可以直接調(diào)用sendXmlData函數(shù)來發(fā)送XML數(shù)據(jù)并處理響應(yīng)結(jié)果。
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;
// 返回處理結(jié)果
$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ù)需要進(jìn)一步處理XML數(shù)據(jù),并創(chuàng)建一個包含您要返回的響應(yīng)的字符串。最后,可以通過調(diào)用receiveXmlData函數(shù)并將結(jié)果輸出來查看處理結(jié)果
結(jié)果:

方法補(bǔ)充
除了上文的方法,小編還為大家整理了其他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);//設(shè)置鏈接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設(shè)置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//設(shè)置HTTP頭
curl_setopt($ch, CURLOPT_POST, 1);//設(shè)置為POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST數(shù)據(jù)
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //設(shè)置超時時間為10
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出錯則顯示錯誤信息
print curl_error($ch);
}
curl_close($ch); //關(guān)閉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;
}到此這篇關(guān)于php通過curl方式實(shí)現(xiàn)發(fā)送接收xml數(shù)據(jù)的文章就介紹到這了,更多相關(guān)php發(fā)送接收xml數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php用戶注冊時常用的檢驗(yàn)函數(shù)實(shí)例總結(jié)
這篇文章主要介紹了php用戶注冊時常用的檢驗(yàn)函數(shù),以類的形式實(shí)例總結(jié)了用戶名驗(yàn)證、郵箱驗(yàn)證、QQ驗(yàn)證等常用的驗(yàn)證技巧,非常具有實(shí)用價值,需要的朋友可以參考下2014-12-12
PHP實(shí)現(xiàn)數(shù)字補(bǔ)零功能的2個函數(shù)介紹
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)字補(bǔ)零功能的2個函數(shù)介紹,需要的朋友可以參考下2014-05-05
php+ajax實(shí)現(xiàn)無刷新數(shù)據(jù)分頁的辦法
這篇文章主要介紹了php+ajax實(shí)現(xiàn)無刷新分頁的方法,詳細(xì)講述了數(shù)據(jù)庫的創(chuàng)建、Ajax文件的實(shí)現(xiàn)及PHP調(diào)用方法,需要的朋友可以參考下2015-11-11

