从 SOAPClient 响应解析命名空间 [英] Parsing namespace from SOAPClient Response

查看:35
本文介绍了从 SOAPClient 响应解析命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我让 SOAPClient 打电话.如果我使用 __getLastResponse() 我会在我的 XML 中得到这样一行:

OK so I have SOAPClient making a call. If I use __getLastResponse() I get a line in my XML like this:

<Discount xsi:type="ProgressivePromotion" from="2013-05-05T00:00:00" to="2013-05-14T00:00:00" type="Percent" value="20" name="Special Deal"/>

但是,我的 SOAPClient 类中的函数返回一个对象.

However, the function in my SOAPClient Class returns an object.

PHP类中调用的代码为:

The code for the call in the PHP class is:

function SearchHotels($parameters ){
$funcRet = null;
try {
    $funcRet = $this->client->SearchHotels($parameters);
} catch ( Exception $e ) {
    echo "(SearchHotels) SOAP Error:\n-------------------------------------\n" . $e->getMessage () . "\n\n";
    echo "(SearchHotels) Request:\n-------------------------------------\n" . $this->client->__getLastRequest() . "\n\n";
    echo "(SearchHotels) Response:\n-------------------------------------\n" . $this->client->__getLastResponse() . "\n\n";
}
return $funcRet; 
}

当我使用返回的对象时,我可以从 Discount 元素访问以下属性:

When I use the object that is returned, I can access the following attributes from the Discount element as:

类型:渐进式促销
来自:2013-05-05T00:00:00
至:2013-05-14T00:00:00
值:20
名称:特价商品

type: ProgressivePromotion
from: 2013-05-05T00:00:00
to: 2013-05-14T00:00:00
value: 20
name: Special Deal

但我无法访问 type="Percent"

But I can't access type="Percent"

似乎 SOAPClient 忽略了 xsi:type 中的 xsi 命名空间,而只是将该属性存储为 type.

It seems that SOAPClient disregards the xsi namespace in xsi:type and just stores that attribute as type.

那么我如何访问 xsi:type AND type 以便我可以判断我的折扣是百分比还是金额还是其他任何类型?

So how can I access xsi:type AND type so I can tell if my discount is a Percent or Amount or whatever other type it could be?

顺便说一句,在我的 SOAP 响应的顶部,我没有看到任何声明 xsi 是什么.

BTW, at the top of my SOAP Response, I do not see ANYTHING declaring what xsi is.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <SearchHotelsResponse xmlns="http://tourico.com/webservices/hotelv3">
            <SearchHotelsResult>
                <Info xmlns="http://schemas.tourico.com/webservices/hotelv3" version="9.71" culture="en-US" serverTime="2013-02-06T14:49:58.3500117-05:00"/>
                   <HotelList xmlns="http://schemas.tourico.com/webservices/hotelv3">

编辑

如果我 var_dump 返回的对象,我得到

If I var_dump the object returned, I get

stdClass Object
(
[SearchHotelsResult] => stdClass Object
    (
        [Info] => stdClass Object
            (
                [version] => 9.71
                [culture] => en-US
                [serverTime] => 2013-02-06T15:17:59.8445748-05:00
            )

        [HotelList] => stdClass Object
            (
                [Hotel] => Array
                    (
                        [0] => stdClass Object
                            [RoomTypes] => stdClass Object
                                    (
                                        [RoomType] => Array
                                            (
                                                [0] => stdClass Object
                                                    (
                                                       [Discount] => stdClass Object
                                                            (
                                                                [from] => 2013-05-05T00:00:00
                                                                [to] => 2013-05-14T00:00:00
                                                                [type] => ProgressivePromotion
                                                                [value] => 20
                                                                [name] => Special Deal
                                                            )

看看我是如何丢失 type="Amount" 的?

See how I lost the type="Amount"?

我不能通过这样做来调用折扣

I can't call Discounts by doing

echo $result->SearchHotelsResult->HotelList->Hotel[0]->RoomTypes->RoomType[0]->Discounts->type;

因为我明白

Undefined property: stdClass::$Discounts  Notice: Trying to get property of non-object

所以我将整个对象转换成一个巨大的多维数组并以这种方式访问​​.无论如何,type="Amount" 不会被拉取.

So I convert the whole object into a giant multidimensional array and access things that way. Regardless, the type="Amount" isn't being pulled.

推荐答案

我通过这种方式解决了这个问题.

i fixed the problem this way.

<?php
class fixSoapClient extends SoapClient{
    function __doRequest($request, $location, $action, $version, $one_way = 0) {
        $result = parent::__doRequest($request, $location, $action, $version);
        $str = preg_replace('#<Discount xsi:type="ProgressivePromotion"(.+)type="(.+)"(.+)name="(.+)"/>#isU', 
                '<Discount xsi:type="ProgressivePromotion"$1type="wath ever"$3name="$2"/>', $result);
        return $str;
    }   
}

您将在名称属性中找到折扣类型.因此,您必须实例化 fixSoapClient

You will find the discount type in the name attribute. So instead of instantiate SoapClient you have to instantiate fixSoapClient

这篇关于从 SOAPClient 响应解析命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆