【问题标题】:Complex type in PHP NuSoapPHP NuSoap 中的复杂类型
【发布时间】:2013-12-16 17:11:16
【问题描述】:

我正在使用 PHP 中的 NuSoap 库构建 Web 服务。我的 Web 服务将充当客户端和供应商现有 Web 服务之间的中间层。因此,客户端不会直接连接到供应商,而是连接到我的 Web 服务,我的 Web 服务连接到供应商并获取响应并将相同的响应发送回客户端。

我唯一的问题是我的供应商正在发回 stdclass 对象(他们的 Web 服务是用 .Net 编写的),我必须接收该对象并通过我的 Web 服务方法将相同的对象发回给客户端。

我在 Internet 上进行了很多搜索,但 NuSoap 库没有明确的方法来实现这一目标。到目前为止,无论我读到什么,都指定我必须使用复杂类型来实现这一点,但我也不知道如何获取响应,然后将其转换为复杂类型并将其发送回客户端。

提前感谢您的帮助。

【问题讨论】:

    标签: php web-services api soap nusoap


    【解决方案1】:

    您所写的内容称为proxy

    网上有some examples供NuSoap服务器通过addComplexType方法发送复杂类型。

    //Create a complex type
    $server->wsdl->addComplexType('MyComplexType','complexType','struct','all','',
    array( 'ID' => array('name' => 'ID','type' => 'xsd:int'),
    'YourName' => array('name' => 'YourName','type' => 'xsd:string')));
    

    实现代理的一种方法是使用存根数据构建服务,这样它实际上不会首先与后端服务对话。看看您是否可以让原始客户对代理的人为响应感到满意。然后,一旦你有了它,使用真正的后端服务应该是微不足道的(根据我的经验,SOAP 客户端操作比服务器操作更容易)。

    另一种选择是考虑使用原生 SoapServer 类。第一条评论here 展示了如何创建复杂类型。

    编辑

    又看了看,这里是much better example

    根据addComplextType (lib/class.wsdl.php) 上的文档块,有两种方法可以向 NuSoap 注册复杂类型

    /**  
    * adds an XML Schema complex type to the WSDL types
    *
    * @param string $name
    * @param string $typeClass (complexType|simpleType|attribute)
    * @param string $phpType currently supported are array and struct (php assoc array)
    * @param string $compositor (all|sequence|choice)
    * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
    * @param array $elements e.g. array ( name => array(name=>'',type=>'') )
    * @param array $attrs e.g. array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'xsd:string[]'))
    * @param string $arrayType as namespace:name (xsd:string)
    * @see nusoap_xmlschema
    * @access public
    */
    

    在我发布的后面的例子中看看他是如何做到的:

    $server->wsdl->addComplexType('Contact',
        'complexType',
        'struct',
        'all',
        '',
        array(
                'id' => array('name' => 'id', 'type' => 'xsd:int'),
                'first_name' => array('name' => 'first_name', 'type' => 'xsd:string'),
                'last_name' => array('name' => 'last_name', 'type' => 'xsd:string'),
                'email' => array('name' => 'email', 'type' => 'xsd:string'),
                'phone_number' => array('name' => 'phone_number', 'type' => 'xsd:string')
        )
    );
    

    那么如何返回Contact复杂类型的响应:

    function updateContact($in_contact) {
        $contact = new Contact($in_contact['id']);
        $contact->first_name=mysql_real_escape_string($in_contact['first_name']);
        $contact->last_name=mysql_real_escape_string($in_contact['last_name']);
        $contact->email=mysql_real_escape_string($in_contact['email']);
        $contact->phone_number=mysql_real_escape_string($in_contact['phone_number']);
        if ($contact->update()) return true;
    }
    

    您还可以在他的示例中看到如何使用数组变体。对不起,答案很重要!

    【讨论】:

    • 感谢您的回复,关于存根数据,我们总是希望从服务器发回最新的响应,这意味着即使供应商 API 已更改或返回不同的输出,我们的 API 也应该反映相同的实时性。因此,在这种情况下,存根数据可能难以使用。来到 nusoap 的复杂类型示例,我是否需要解析来自服务器的对象,然后在数组中重新分配它以构建复杂类型?我也可以在我的 API 中的方法内构建复杂类型,还是必须在方法外?
    • 啊,实施起来可能有点痛苦,但我听说你在存根数据上。对于复杂类型,您要注册复杂类型,然后在处理请求的方法中(上面第二个示例中的updateContact)客户端将发送您想要使用soapclient 连接到后端服务。解析来自它的响应,创建对象或数组(基于addComplexType 中的structarray 选项,然后从处理程序返回。
    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多