You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP初学者求助:基于NuSOAP实现SOAP服务返回多维产品数组

最简NuSOAP实现:返回指定分类的产品列表

嘿,作为PHP初学者能想到用NuSOAP做带WSDL的SOAP服务,已经迈出不错的一步了!针对你需要返回的特定SOAP响应格式,我整理了一套直接能用的最简实现,你只需要替换掉模拟的产品数据为你的MySQL查询结果就行:

第一步:准备NuSOAP库

先把NuSOAP库下载下来(找最新稳定版即可),解压后把整个文件夹放到你的项目目录里,比如命名为nusoap,确保服务端脚本能引用到里面的nusoap.php。

第二步:编写SOAP服务端脚本(getProduct.php)

这是核心部分,完全适配你要求的SOAP格式:

<?php
// 引入NuSOAP库
require_once './nusoap/lib/nusoap.php';

// 创建SOAP服务端实例,指定你要求的命名空间
$server = new soap_server();
$server->configureWSDL('ProductService', 'http://localhost/sttpb/services/getProduct.php');
$server->wsdl->schemaTargetNamespace = 'http://localhost/sttpb/services/getProduct.php';

// 注册getProduct方法,定义输入输出参数
$server->register(
    'getProduct',
    array('category' => 'xsd:string'),
    array('return' => 'xsd:string'),
    'http://localhost/sttpb/services/getProduct.php',
    'http://localhost/sttpb/services/getProduct.php#getProduct',
    'rpc',
    'encoded',
    '获取指定分类下的产品列表'
);

// 核心业务函数:处理分类参数,生成指定格式的XML
function getProduct($category) {
    // --------------------------
    // 这里替换成你的MySQL查询代码
    // 假设你已经拿到了多维数组$products,结构如下:
    $products = [
        ['id' => 1, 'designation' => 'Product 1', 'price' => 10],
        ['id' => 2, 'designation' => 'Product 2', 'price' => 10],
        ['id' => 3, 'designation' => 'Product 3', 'price' => 10]
    ];
    // --------------------------

    // 拼接符合要求的XML结构
    $xml = '';
    foreach ($products as $product) {
        $xml .= '<Product>';
        $xml .= '<id>' . $product['id'] . '</id>';
        $xml .= '<designation>' . htmlspecialchars($product['designation']) . '</designation>';
        $xml .= '<price>' . $product['price'] . '</price>';
        $xml .= '</Product>';
    }

    // 返回拼接好的XML字符串,NuSOAP会自动包装到指定的SOAP Envelope中
    return $xml;
}

// 处理客户端请求
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>

关键细节说明

  • 手动拼接XML的原因:你要求的响应格式里,<return>节点下直接包含多个<Product>元素,而NuSOAP默认处理数组会生成带索引的结构,手动拼接能精准匹配你要的格式。
  • htmlspecialchars()的作用:避免产品名称里的特殊字符(比如<、&)破坏XML结构,防止注入问题。
  • 命名空间完全按照你给出的地址设置,确保和SOAP请求的命名空间一致,避免兼容问题。

第三步:测试SOAP客户端

用下面的代码快速验证服务是否正常:

<?php
require_once './nusoap/lib/nusoap.php';

// 创建SOAP客户端实例
$client = new nusoap_client('http://localhost/sttpb/services/getProduct.php?wsdl', true);

// 检查初始化错误
$err = $client->getError();
if ($err) {
    echo '<h2>初始化错误</h2><pre>' . $err . '</pre>';
    exit;
}

// 调用getProduct方法,传入分类参数'X'
$result = $client->call('getProduct', array('category' => 'X'));

// 检查调用结果
if ($client->fault) {
    echo '<h2>调用错误</h2><pre>';
    print_r($result);
    echo '</pre>';
} else {
    $err = $client->getError();
    if ($err) {
        echo '<h2>响应错误</h2><pre>' . $err . '</pre>';
    } else {
        echo '<h2>返回结果</h2><pre>';
        print_r($result);
        echo '</pre>';
    }
}
?>

运行这个客户端脚本,就能看到完全符合你要求格式的产品列表响应了。

内容的提问来源于stack exchange,提问作者Samir Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:02:39