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函数并将结果输出来查看处理结果

结果:

相关推荐
曦月逸霜13 分钟前
离散数学-学习笔记(持续更新中~)
笔记·学习·离散数学
hunter145024 分钟前
windows server AD域与CA部署证书
笔记
im_AMBER25 分钟前
Leetcode 101 对链表进行插入排序
数据结构·笔记·学习·算法·leetcode·排序算法
laplace01231 小时前
# 第四章|智能体经典范式构建 —— 学习笔记(详细版)
笔记·学习
别了,李亚普诺夫1 小时前
PCB设计大师篇笔记
笔记·嵌入式硬件·学习
西瓜堆1 小时前
提示词工程学习笔记: IT技术行业提示词推荐
笔记·学习
2301_810746311 小时前
CKA冲刺40天笔记 - day24 Kubernetes Clusterrole 和 Clusterrole Binding
笔记·容器·kubernetes
IT=>小脑虎1 小时前
PHP零基础衔接进阶知识点【详解版】
开发语言·学习·php
stars-he1 小时前
FPGA学习笔记-图书馆存包柜,乒乓球游戏电路设计
笔记·学习·fpga开发
Gary Studio2 小时前
simulink simscape(机器人方向)学习笔记
笔记·学习