我在PHP的SoapClient中使用命名空间时遇到了一些问题.从文档中我相信构造函数的typemap选项将解决我的问题.
http://php.net/manual/en/soapclient.soapclient.php
我还没有找到它的用法的一个坚实的例子.
有人有例子吗?
Hai*_*ian 13
这是一个从测试源中获取的简单示例,其中添加了我的注释:
<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>";
Run Code Online (Sandbox Code Playgroud)
// 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);
Run Code Online (Sandbox Code Playgroud)
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',
'type_name' => 'book',
// myStaticCallbackMethod must be a public static function of MyClass
'from_xml' => array('\My\Name\Space\MyClass', 'myStaticCallbackMethod'),
),
Run Code Online (Sandbox Code Playgroud)
或者PHP PHP 5.2.3及以上版本:
array(
// type namespaces have to match those declared in the WSDL
'type_ns' => 'http://schemas.nothing.com',
'type_name' => 'book',
// myStaticCallbackMethod must be a public static function of MyClass
'from_xml' => array('\My\Name\Space\MyClass::myStaticCallbackMethod'),
),
Run Code Online (Sandbox Code Playgroud)
类定义:
namespace My\Name\Space;
class MyClass
{
public static function myStaticCallbackMethod($xml)
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
对象方法调用
$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",
'type_name' => 'book',
'from_xml' => array($obj, 'myCallbackMethod'),
),
Run Code Online (Sandbox Code Playgroud)
类定义:
namespace My\Name\Space;
class MyClass
{
public function myCallbackMethod($xml)
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
关闭
$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',
'type_name' => 'book',
'from_xml' => $myCallback,
),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9206 次 |
| 最近记录: |