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

时间:2009-12-14 21:56:00

标签: php soap

鉴于以下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>

如何访问参数的名称? (requestId和PartnerId)在server.php中?名称显然位于有效负载中,但在服务器端仅接收值(1和99)

示例代码如下:

client.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());

server.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,这可能不对。

1 个答案:

答案 0 :(得分:1)

当我遇到这个问题时,我终于决定采用代理功能的想法 - 使用名为($ 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消息的一个元素。