我在
PHP的SoapClient中使用命名空间有一个小问题.
从文档中我相信构造函数的typemap选项可以解决我的问题.
从文档中我相信构造函数的typemap选项可以解决我的问题.
http://php.net/manual/en/soapclient.soapclient.php
我还没有找到一个很好的例子.
有人有例子吗?
这里有一个简单的例子,从测试源中添加了我的注释:
原文链接:https://www.f2er.com/php/139452.htmlSOAP请求
<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:enc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://schemas.nothing.com\" > <env:Body> <ns1:dotest> <book xsi:type=\"ns1:book\"> <a xsi:type=\"xsd:string\">foo</a> <b xsi:type=\"xsd:string\">bar</b> </book> </ns1:dotest> </env:Body> <env:Header/> </env:Envelope>";
// data object class class book{ public $a="a"; public $b="c"; } // XML transform callback function (for converting the "type" into an object) function book_from_xml($xml) { $sxe = simplexml_load_string($xml); $obj = new book; $obj->a = (string)$sxe->a; $obj->b = (string)$sxe->b; return $obj; } // SOAP action class (called by soap handle() method) class test { function dotest($book) { $classname=get_class($book); return "Object: ".$classname. "(".$book->a.",".$book->b.")"; } } // SOAPServer Instantiation $options=Array( 'actor' =>'http://schemas.nothing.com','typemap' => array( array( // type namespaces have to match those declared in the WSDL 'type_ns' => 'http://schemas.nothing.com','type_name' => 'book','from_xml' => 'book_from_xml',),// additional typemap definition arrays go here ) ); $server = new SoapServer(dirname(__FILE__)."/classmap.wsdl",$options); $server->setClass("test"); $server->handle($HTTP_RAW_POST_DATA);
有用但重要的说明
>输入代码必须定义一个from_xml回调,否则您将收到一个细分错误.
>输出类型映射必须定义一个to_xml回调,否则您将收到一个细分错误.
> type_ns命名空间值必须与您在WSDL中定义的文字命名空间相匹配,否则不会发生类型匹配.
>回调可能比上述示例中使用的简单函数回调更复杂.支持类/对象方法.见下文.
复数回调
当使用对象或类方法进行回调时,您需要确保使用类的FQCN(如果您使用命名空间),或者a)如果不想实例化,请将回调方法声明为public static一个实例,或者b)先创建对象的一个实例,并使用它的方法作为回调.
以下是typemap数组中更复杂的回调的示例:
... array( // type namespaces have to match those declared in the WSDL 'type_ns' => 'http://schemas.nothing.com',// myStaticCallbackMethod must be a public static function of MyClass 'from_xml' => array('\My\Name\Space\MyClass','myStaticCallbackMethod'),
array( // type namespaces have to match those declared in the WSDL 'type_ns' => 'http://schemas.nothing.com',// myStaticCallbackMethod must be a public static function of MyClass 'from_xml' => array('\My\Name\Space\MyClass::myStaticCallbackMethod'),
类定义:
namespace My\Name\Space; class MyClass { public static function myStaticCallbackMethod($xml) { // do something } }
$obj = new \My\Name\Space\MyClass(); ... // static class method call array( // type namespaces have to match those declared in the WSDL 'type_ns' => "http://schemas.nothing.com",'from_xml' => array($obj,'myCallbackMethod'),
类定义:
namespace My\Name\Space; class MyClass { public function myCallbackMethod($xml) { // do something } }
$myCallback = function($xml) { // do something }; ... // static class method call array( // type namespaces have to match those declared in the WSDL 'type_ns' => 'http://schemas.nothing.com','from_xml' => $myCallback,