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

目录

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

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


1、php通过curl方式发送xml数据
php 复制代码
<?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 复制代码
<?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函数并将结果输出来查看处理结果

结果:

相关推荐
BingoGo1 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
JaguarJack1 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
用户3074596982072 天前
PHP 扩展——从入门到理解
php
鹏仔先生3 天前
拷贝漫画APP下载页PHP程序,后台带免费AI写作
php
云水一下3 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
闪闪发亮的小星星3 天前
高斯光以及高斯光公式解释
笔记
xingpanvip3 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
cqbzcsq3 天前
CellFlow虚拟细胞论文阅读
论文阅读·人工智能·笔记·学习·生物信息
阿米亚波3 天前
【Windows】QEMU 启动 openEuler aarch64/arm64 架构系统 + 离线软件源
linux·windows·经验分享·笔记·架构·arm
自传.3 天前
尚硅谷 Vibe Coding|第三章(1) Claude Code深度使用与进阶技巧 学习笔记
笔记·学习·尚硅谷·vibecoding