PHP SoapClient示例使用typemap选项
发布时间:2020-12-13 13:59:34 所属栏目:PHP教程 来源:网络整理
导读:我在 PHP的SoapClient中使用命名空间有一个小问题. 从文档中我相信构造函数的typemap选项可以解决我的问题. http://php.net/manual/en/soapclient.soapclient.php 我还没有找到一个很好的例子. 有人有例子吗? 这里有一个简单的例子,从测试源中添加了我的注
我在
PHP的SoapClient中使用命名空间有一个小问题.
从文档中我相信构造函数的typemap选项可以解决我的问题. http://php.net/manual/en/soapclient.soapclient.php 我还没有找到一个很好的例子. 有人有例子吗?
这里有一个简单的例子,从测试源中添加了我的注释:
SOAP请求 <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); Source Reference File 有用但重要的说明 >输入代码必须定义一个from_xml回调,否则您将收到一个细分错误. 复数回调 当使用对象或类方法进行回调时,您需要确保使用类的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('MyNameSpaceMyClass','myStaticCallbackMethod'), 或PHP版本PHP 5.2.3及以上版本: 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('MyNameSpaceMyClass::myStaticCallbackMethod'), 类定义: namespace MyNameSpace; class MyClass { public static function myStaticCallbackMethod($xml) { // do something } } 对象方法调用 $obj = new MyNameSpaceMyClass(); ... // 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 MyNameSpace; 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, (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |