php通过curl方式实现发送接收xml数据

 更新时间:2023年11月24日 09:27:50   作者:PHP隔壁老王邻居  
这篇文章主要为大家详细介绍了php如何通过curl方式实现发送接收xml数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

1、php通过curl方式发送xml数据

<?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;

发送XML数据的函数名为sendXmlData,它接受两个参数:$url是目标服务器的URL,$xmlData是要发送的XML数据。函数内部使用curl函数发送HTTP POST请求,并返回服务器的响应。您可以直接调用sendXmlData函数来发送XML数据并处理响应结果。

2、php通过file_get_contents接受curl方式发送xml数据

<?php
 
function receiveXmlData() {
    $xmlData = file_get_contents('php://input');
    // 解析XML数据
    $xml = simplexml_load_string($xmlData);
    // 处理XML数据
    // 例如,获取根元素的值
    $name = $xml->name;
    $age = $xml->age;
    // 返回处理结果
    $response = "Received XML Data:\nName: $name\nAge: $age";
    return $response;
}
// 使用示例
$response = receiveXmlData();
echo $response;

函数receiveXmlData从输入流(php://input)中获取接收到的XML数据,并使用simplexml_load_string函数将其解析为可操作的XML对象。您可以根据需要进一步处理XML数据,并创建一个包含您要返回的响应的字符串。最后,可以通过调用receiveXmlData函数并将结果输出来查看处理结果

结果:

方法补充

除了上文的方法,小编还为大家整理了其他PHP接收发送XML数据的方法,希望对大家有所帮助

使用CURL发送xml数据

<?PHP
$xml_data = '<xml>xml</xml>';//发送的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数据
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数据

<?php
$xml = file_get_contents('php://input');//监听是否有数据传入
if($xml!=''){//如果有数据传入,则将传入的数据写入到xml.txt文件
$fp = fopen('xml.txt','w');
fwrite($fp,$xml);
fclose($fp);
exit('OK');//写入成功后返回成功信息
}
die('Fail');//没有数据则直接返回失败信息

php发送和接收json、xml数据

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方式实现发送接收xml数据的文章就介绍到这了,更多相关php发送接收xml数据内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

最新评论

?


http://www.vxiaotou.com