当前位置: 代码网 > it编程>编程语言>Php > php通过curl方式实现发送接收xml数据

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

2024年05月18日 Php 我要评论
1、php通过curl方式发送xml数据<?phpfunction sendxmldata($url, $xmldata) { $ch = curl_init(); curl_set

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数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com