dongle7553 2019-06-05 18:07
浏览 92

SOAP函数返回一个stdClass

I get an PHP error eveyrtime I try to access to an object property from SOAP WSDL.

I guess it's supposed to return an associativeArray type, but I've tried to do a print_r and the stdClass is still there.

This is the WSDL structure for the product object:

<complexType name="catalogProductReturnEntity">
<all>
<element name="product_id" type="xsd:string" minOccurs="0"/>
<element name="sku" type="xsd:string" minOccurs="0"/>
<element name="set" type="xsd:string" minOccurs="0"/>
<element name="type" type="xsd:string" minOccurs="0"/>
<element name="categories" type="typens:ArrayOfString" minOccurs="0"/>
<element name="websites" type="typens:ArrayOfString" minOccurs="0"/>
<element name="created_at" type="xsd:string" minOccurs="0"/>
<element name="updated_at" type="xsd:string" minOccurs="0"/>
<element name="type_id" type="xsd:string" minOccurs="0"/>
<element name="name" type="xsd:string" minOccurs="0"/>
<element name="description" type="xsd:string" minOccurs="0"/>
<element name="short_description" type="xsd:string" minOccurs="0"/>
<element name="weight" type="xsd:string" minOccurs="0"/>
<element name="status" type="xsd:string" minOccurs="0"/>
<element name="url_key" type="xsd:string" minOccurs="0"/>
<element name="url_path" type="xsd:string" minOccurs="0"/>
<element name="visibility" type="xsd:string" minOccurs="0"/>
<element name="category_ids" type="typens:ArrayOfString" minOccurs="0"/>
<element name="website_ids" type="typens:ArrayOfString" minOccurs="0"/>
<element name="has_options" type="xsd:string" minOccurs="0"/>
<element name="gift_message_available" type="xsd:string" minOccurs="0"/>
<element name="price" type="xsd:string" minOccurs="0"/>
<element name="special_price" type="xsd:string" minOccurs="0"/>
<element name="special_from_date" type="xsd:string" minOccurs="0"/>
<element name="special_to_date" type="xsd:string" minOccurs="0"/>
<element name="tax_class_id" type="xsd:string" minOccurs="0"/>
<element name="tier_price" type="typens:catalogProductTierPriceEntityArray" minOccurs="0"/>
<element name="meta_title" type="xsd:string" minOccurs="0"/>
<element name="meta_keyword" type="xsd:string" minOccurs="0"/>
<element name="meta_description" type="xsd:string" minOccurs="0"/>
<element name="custom_design" type="xsd:string" minOccurs="0"/>
<element name="custom_layout_update" type="xsd:string" minOccurs="0"/>
<element name="options_container" type="xsd:string" minOccurs="0"/>
<element name="additional_attributes" type="typens:associativeArray" minOccurs="0"/>
</all>
</complexType>

and this is the code I'm trying:

$product = $proxy->catalogProductInfo($sessionId, 38882);
print_r($product->additional_attributes);

All the properties except the additional_attributes (which is the one that returns the stdClass) one work perfectly.

The server returns the following when I try to access additional_attributes:

Notice: Undefined property: stdClass::$additional_attributes in /Applications/XAMPP/xamppfiles/htdocs/php_test/test.php on line 23

  • 写回答

1条回答 默认 最新

  • dongya9904 2019-06-06 09:51
    关注

    Per definition the php soap client returns stdClass objects based on the xml definitions / wsdl types it uses. The recieved stdClass object normally contains all attributes described in the definition. As you can see there 's a minoccurs="0" attribute in the definition for the additional_attributes attribute. This means it can occur, but don 't has to. So the result depends on the values recieved in the xml response.

    To avoid such behaviour just work with php classes like in the following example. The php soap client accepts a classmap parameter, which tells the client which complex type to use with a defined class.

    The Entity

    class CatalogProductReturnEntity
    {
        ...
        public $additional_attributes;
        ...
    }
    

    Just write down an entity class with all properties, that are defined in the xml definition. This class can be used as an entity for the soap client. Using this entity class avoids the PHP notice, that a property is undefined.

    The Soap Client with a Class Map

    The next step is telling the soap client, which classes to use with the complex types.

    $client = new \SoapClien($wsdl, [
        'classmap' => [
            'catalogProductReturnEntity' => CatalogProductReturnEntity::class,
        ],
    ]);
    
    $product = $client->catalogProductInfo($sessionId, 38882);
    

    Now the client knows, which class to use, when recieving a complex type with the name catalogProductReturnEntity. Since our entity class has a property called additional_attributes you can easily call it with $product->additiona_attributes. If there wasn 't recieved a value for the additional_attributes property, the value will be null.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格