gpt4 book ai didi

php - 使用 PHP SOAP 扩展从 SOAP 请求中获取参数名称?

转载 作者:可可西里 更新时间:2023-10-31 23:44:27 25 4
gpt4 key购买 nike

鉴于以下 client.php 创建此请求 XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://soap.dev/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:Test>
<RequestId xsi:type="xsd:int">1</RequestId>
<PartnerId xsi:type="xsd:int">99</PartnerId>
</ns1:Test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如何访问参数的名称? server.php 中的(RequestId 和 PartnerId)?有效负载中清楚地显示了名称,但在服务器端仅接收到值(1 和 99)

示例代码如下:

客户端.php

<?php
$client_params = array(
'location' => 'http://soap.dev/server.php',
'uri' => 'http://soap.dev/',
'trace' => 1
);

$client = new SoapClient(null, $client_params);

try {
$res = $client->Test(
new SoapParam(1, 'RequestId'),
new SoapParam(99, 'PartnerId')
);

} catch (Exception $Ex) {
print $Ex->getMessage();
}

var_dump($client->__getLastRequest());
var_dump($client->__getLastResponse());

服务器.php

class receiver {
public function __call ($name, $params)
{
$args = func_get_args();
// here $params equals to array(1, 99)
// I want the names as well.
return var_export($args, 1);
}

}

$server_options = array('uri' => 'http://soap.dev/');
$server = new SoapServer(null, $server_options);
$server->setClass('receiver');
$server->handle();

请注意,我无法真正更改传入的请求格式。

此外,我知道我可以通过使用 $RequestId$PartnerId 参数创建一个测试函数,将名称返回给参数。

但我真正想要的是从传入请求中获取名称/值对。

到目前为止,我唯一的想法就是简单地解析 XML,这是不对的。

最佳答案

当我遇到这个问题时,我最终决定采用代理函数的想法 - 使用名为($RequestId 和 $PartnerId)的参数测试函数,以将名称返回给参数。它是适当的解决方案,但肯定不是最好的。

不过,我还没有失去找到更好解决方案的希望,这是迄今为止我最好的主意

<?php
class Receiver {

private $data;

public function Test ()
{
return var_export($this->data, 1);
}

public function int ($xml)
{
// receives the following string
// <PartnerId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:int">99</PartnerId>
$element = simplexml_load_string($xml);
$this->data[$element->getName()] = (string)$element;
}

}

$Receiver = new Receiver();

$int = array(
'type_name' => 'int'
, 'type_ns' => 'http://www.w3.org/2001/XMLSchema'
, 'from_xml' => array($Receiver, 'int')
);


$server_options = array('uri' => 'http://www.w3.org/2001/XMLSchema', 'typemap' => array($int), 'actor' => 'http://www.w3.org/2001/XMLSchema');
$server = new SoapServer(null, $server_options);
$server->setObject($Receiver);
$server->handle();

它仍然意味着手动解析 XML,但一次解析一个元素,这比解析整个传入的 SOAP 消息更理智。

关于php - 使用 PHP SOAP 扩展从 SOAP 请求中获取参数名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1903775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com