通过PHP SoapClient使用ComplexType的对象

时间:2010-08-17 16:08:06

标签: php xml soap wsdl complextype

我正在尝试使用PHP SoapClient使用soap feed并将其转换为数组。它运行良好,直到我遇到一个复杂类型的对象。

然后它返回......

[0] => stdClass对象

...而不是显示我访问SOAP提要时显示的多个元素。

我对SOAP很新,所以感谢任何帮助。

WSDL的相关部分看起来像这样......

<xs:complexType name="UserInfo"> 
            <xs:complexContent> 
                <xs:extension base="tns:UserBaseInfo"> 
                    <xs:sequence> 
                        <xs:element minOccurs="0" name="contact_name" type="xs:string"/> 
                        <xs:element minOccurs="0" name="group_name" type="xs:string"/> 
                        <xs:element name="active" type="xs:boolean"/> 
                        <xs:element name="validated" type="xs:boolean"/> 
                        <xs:element name="deleted" type="xs:boolean"/> 
                        <xs:element name="hidden" type="xs:boolean"/> 
                        <xs:element minOccurs="0" name="deleted_date" type="xs:dateTime"/> 
                        <xs:element minOccurs="0" name="pref_contact" type="xs:string"/> 
                        <xs:element minOccurs="0" name="last_login_date" type="xs:dateTime"/> 
                        <xs:element minOccurs="0" name="last_modify" type="xs:dateTime"/> 
                        <xs:element minOccurs="0" name="updated_by" type="xs:string"/> 
                        <xs:element name="no_mass_email" type="xs:boolean"/> 
                        <xs:element minOccurs="0" name="pref_bb_contact" type="xs:string"/> 
                        <xs:element minOccurs="0" name="pref_bb_image" type="xs:string"/> 
                        <xs:element maxOccurs="unbounded" minOccurs="0" name="attribute_list" type="tns:UserAttInfo"/> 
                    </xs:sequence> 
                </xs:extension> 
            </xs:complexContent> 
        </xs:complexType> 

        <xs:complexType name="UserAttInfo"> 
            <xs:sequence> 
                <xs:element name="user_id" type="xs:string"/> 
                <xs:element name="att_id" type="xs:string"/> 
                <xs:element minOccurs="0" name="att_name" type="xs:string"/> 
                <xs:element minOccurs="0" name="att_type_id" type="xs:string"/> 
                <xs:element minOccurs="0" name="att_type_name" type="xs:string"/> 
                <xs:element minOccurs="0" name="att_data" type="xs:string"/> 
                <xs:element name="hidden" type="xs:boolean"/> 
                <xs:element minOccurs="0" name="last_modify" type="xs:dateTime"/> 
                <xs:element name="view_only" type="xs:boolean"/> 
                <xs:element name="editable" type="xs:boolean"/> 
            </xs:sequence> 
        </xs:complexType> 

我的PHP代码看起来像这样......

$wsdl = new SoapClient('https://location.com/UserService?wsdl', array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));


    $data = $wsdl->$method($params);

    $result['gusername']            = $username;
    $result['password']             = $password;

    $result['user_id']              = $data->return->user_id;           // 8 digits integer <user_id>16977408</user_id>
    $result['group_id']             = $data->return->group_id;          // 6 digits integer
    $result['org_id']               = $data->return->org_id;            // 3 letter string
    $result['contact_name']         = $data->return->contact_name;      // string
    $result['active']               = $data->return->active;            // boolean
    $result['validated']            = $data->return->validated;         // boolean
    $result['deleted']              = $data->return->deleted;           // boolean
    $result['hidden']               = $data->return->hidden;            // boolean
    $result['last_login_date']      = $data->return->last_login_date;   // date string
    $result['last_modify']          = $data->return->last_modify;       // date string
    $result['updated_by']           = $data->return->updated_by;        // string
    $result['no_mass_email']        = $data->return->no_mass_email;     // boolean
    $result['pref_bb_contact']      = $data->return->pref_bb_contact;   // string
    $result['pref_bb_image']        = $data->return->pref_bb_image;     // string
    $result['attribute_list']       = $data->return->attribute_list; 

结果输出如下所示......

    Array
(
    [gusername] => jsmith
    [password] => password
    [user_id] => 123456789
    [group_id] => 123456
    [org_id] => obs
    [contact_name] => John Smith
    [active] => 1
    [validated] => 1
    [deleted] => 
    [hidden] => 
    [last_login_date] => 2010-08-17T14:39:41Z
    [last_modify] => 2010-03-29T22:43:42Z
    [updated_by] => John Smith
    [no_mass_email] => 
    [pref_bb_contact] => 
    [pref_bb_image] => 
    [attribute_list] => Array
        (
            [0] => stdClass Object
                (
                )

        )

)

这是原始xml输出的样子......

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
    -<soapenv:Body>
        -<authenticateUserResponse xmlns="http://ws.domain.com/xsd">
        -<return>
            -<user_id>12345679</user_id>
            -<group_id>100400</group_id>
            -<org_id>obs</org_id>
            -<contact_name>John Smith</contact_name>
            -<active>true</active>
            -<validated>true</validated>
            -<deleted>false</deleted>
            -<hidden>false</hidden>
            -<last_login_date>2010-08-17T14:39:41Z</last_login_date>
            -<last_modify>2010-03-29T22:43:42Z</last_modify>
            -<updated_by>John Smith</updated_by>
            -<no_mass_email>false</no_mass_email>
            -<pref_bb_contact></pref_bb_contact>
            -<pref_bb_image></pref_bb_image>
            -<attribute_list>
                -<UserAttInfo>
                    -<user_id>123456798</user_id>
                    -<att_id>406833</att_id>
                    -<att_name>Contact Name</att_name>
                    -<att_type_id>16</att_type_id>
                    -<att_type_name>Contact Center Greeting</att_type_name>
                    -<att_data>John Smith</att_data>
                    -<hidden>false</hidden>
                    -<last_modify>2009-11-11T22:13:13Z</last_modify>
                    -<view_only>false</view_only>
                    -<editable>true</editable>
                </UserAttInfo>
                -<UserAttInfo>
                    -<user_id>16977408</user_id>
                    -<att_id>406824</att_id>
                    -<att_name>Email</att_name>
                    -<att_type_id>1</att_type_id>
                    -<att_type_name>E-mail Address (Contact Center)</att_type_name>
                    -<att_data>jsmith@domain.com</att_data>
                    -<hidden>false</hidden>
                    -<last_modify>2009-11-11T22:13:13Z</last_modify>
                    -<view_only>false</view_only>
                    -<editable>true</editable>
                </UserAttInfo>
            </attribute_list>
        </return>
        </authenticateUserResponse>
    </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

XML:

<ns1:Products>
    <ns1:WebProductInPurchaseInfo>
        <ns1:Amount>65</ns1:Amount>
        <ns1:Price>65</ns1:Price>
        <ns1:DiscountAmount>0</ns1:DiscountAmount>
        <ns1:Position>1</ns1:Position>
        <ns1:Present>0</ns1:Present>
        <ns1:ProductCode>1182</ns1:ProductCode>
        <ns1:Quantity>5</ns1:Quantity>
    </ns1:WebProductInPurchaseInfo>
    <ns1:WebProductInPurchaseInfo>
        <ns1:Amount>65</ns1:Amount>
        <ns1:Price>65</ns1:Price>
        <ns1:DiscountAmount>0</ns1:DiscountAmount>
        <ns1:Position>2</ns1:Position>
        <ns1:Present>0</ns1:Present>
        <ns1:ProductCode>1174</ns1:ProductCode>
        <ns1:Quantity>3</ns1:Quantity>
    </ns1:WebProductInPurchaseInfo>
</ns1:Products>

PHP:

$stdData->Products = new ArrayObject();
foreach($basket->product_list as $i => $obj) {

        $product = new stdClass();

        $product->Amount         = $obj->price;
        $product->Price          = $obj->discount_price;
        $product->DiscountAmount = $basket->discount;
        $product->Position       = $k;
        $product->Present        = 0;
        $product->ProductCode    = $obj->code;
        $product->Quantity       = $obj->count;

        foreach($product as $k => $v) {
            $product->$k = new SoapVar($v, XSD_ANYTYPE, null, null, $k, 'namespaces');
        }

        $product = new SoapVar($product, SOAP_ENC_OBJECT, null, null, 'WebProductInPurchaseInfo', 'namespaces');

        $stdData->Products->append($product);
    }

$stdData->Products = new SoapVar($stdData->Products, SOAP_ENC_OBJECT, null, null);