PHP SoapClient示例使用typemap选项

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP SoapClient示例使用typemap选项脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 PHPSOApClient中使用命名空间有一个小问题.
从文档中我相信构造函数的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回调,否则您将收到一个细分错误.
>输出类型映射必须定义一个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'),

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('\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,

脚本宝典总结

以上是脚本宝典为你收集整理的PHP SoapClient示例使用typemap选项全部内容,希望文章能够帮你解决PHP SoapClient示例使用typemap选项所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。