0

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

josembell
  • 29
  • 4

1 Answers1

0

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.

Marcel
  • 4,854
  • 1
  • 14
  • 24