2009-12-14 181 views
4

鉴于以下client.php创建此请求XML:使用PHP SOAP扩展从SOAP请求获取参数名称?

<?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

当我遇到这个问题时,我终于决定使用代理函数的思想 - 测试函数的参数名称为($ 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消息。