Use WsdlToPhp to manage your Magento website with its SOAP API

  18 July 2013       This post has 1 comment       By Mikaël DELSOL


Your server must run the latest PHP version with the native SoapClient class.

For this sample, we use the generated package associated to the Magento Web service. This package and all the following samples have been made with the Magento official version from

In order to access to the WSDL of your Magento website, please refer to the official online documentation for SOAP.

You should use the URL containing the string api/v2_soap?wsdl=1 so the complete path to the WSDL on your Magento website should look like this :

The generated package includes :

  • the sample file: the file that shows you how to start using the generated package
  • the autoload file: the file which loads all the generated class with the good path
  • the main WsdlClass class: the class from which each generated class inherits in order to inherit usefull methods and generic methods to deal with the SoapClient
  • the ClassMap class: the class which defines the mapping between native Magento Web service structs and generated classes
  • all the classes required to communicate with the Magento SOAP API

If you have a good IDE, then it should be really easy to navigate through all these numerous classes.

Call Magento SOAP API operations with WsdlToPhp

First step: give access to your Magento SOAP API

The Magento SOAP Web service requires that you create a session id by calling the Magento login operation with an username and an api key. These informations are related to an user that has to be associated to your Magento SOAP Web service.

First of all, you must create at least one role under the menu System > Web Services > SOAP/XML-RPC - Roles and then create at least one user associated to the role under the menu  System > Web Services > SOAP/XML-RPC - Users.

Be sure to give enough rights to the role you just created so you can call any operation we are going to. In any case, you'll able to update the rights afterwards so don't panic.

Second step: load classes and set configuration

The generated package includes an autoload file which makes easy to load all the generated classes at once.

require_once __DIR__ . '/MagentoAutoload.php';

We suppose your file is located in the root directory of the extracted package you just downloaded.

When all classes are loaded, you can define the configuration to call the SOAP Web service. This configuration is required to instantiate any Magento Service class.

$wsdl = array(); $wsdl[MagentoWsdlClass::WSDL_URL] = ''; $wsdl[MagentoWsdlClass::WSDL_CACHE_WSDL] = WSDL_CACHE_NONE; // It is required to use the SOAP 1.2 client $wsdl[MagentoWsdlClass::WSDL_SOAP_VERSION] = SOAP_1_2; $wsdl[MagentoWsdlClass::WSDL_TRACE] = true;

Third step: create a session id

In order to call any SOAP operation on your Magento website, you need to be logged in with the username and api key you created previously.

To create a session id, you have to call the login operation. The sample code below shows how to achieve this operation:

$magentoServiceLogin = new MagentoServiceLogin($wsdl); if($magentoServiceLogin->login(new MagentoStructString('***username***'),new MagentoStructString('***api key***'))) { // The session id is contained by the response itself. // We just encapsulate it in a MagentoStructString object because this is this object // that will be used as the session id parameter for the next operation calls. $sessionId = new MagentoStructString($magentoServiceLogin->getResult()); } else     print_r($magentoServiceLogin->getLastError());

The XML request looks like this:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:xsd="" xmlns:xsi="" xmlns:enc=""> <env:Body> <ns1:login env:encodingStyle=""> <username xsi:type="enc:string">***username***</username> <apiKey xsi:type="enc:string">***api key***</apiKey> </ns1:login> </env:Body> </env:Envelope>

The XML response looks like this:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:xsd="" xmlns:xsi="" xmlns:enc=""> <env:Body xmlns:rpc=""> <ns1:loginResponse env:encodingStyle=""> <rpc:result>loginReturn</rpc:result> <loginReturn xsi:type="xsd:string">809bfa6b501703e94560863867b60164</loginReturn> </ns1:loginResponse> </env:Body> </env:Envelope>

As you can see, the session id is contained by the loginReturn XML tag and is simply retrieved by calling the $magentoServiceLogin->getResult() method.

If you don't succeed to retrieve a valid session id then look to the last generated error by calling the $magentoServiceLogin->getLastError() method.

From now, we are able to call any operation using the $sessionId variable created.

First service call: retrieve your Magento version and edition

To do so, we are going to call the Magento Info operation. This operation is detailed on the official Magento Info API page for Magento Info and it should return the version and the edition of the Magento website.

Proceed like this:

$magentoServiceMagento = new MagentoServiceMagento($wsdl); $magentoInfo = $magentoServiceMagento->magentoInfo($sessionId); 

This simple call generates this XML request:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:xsd="" xmlns:xsi="" xmlns:enc="">   <env:Body>     <ns1:magentoInfo env:encodingStyle="">       <sessionId xsi:type="xsd:string">ddaac4a055beaf2fdef0e810c9a4d307</sessionId>       <param1 xsi:nil="true"/>       <param2 xsi:nil="true"/>     </ns1:magentoInfo>   </env:Body> </env:Envelope> 

That should return this XML response:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:xsd="" xmlns:xsi="" xmlns:enc="">   <env:Body xmlns:rpc="">     <ns1:magentoInfoResponse env:encodingStyle="">       <rpc:result>info</rpc:result>       <info xsi:type="ns1:magentoInfoEntity">         <magento_version xsi:type="xsd:string"></magento_version>         <magento_edition xsi:type="xsd:string">Community</magento_edition>       </info>     </ns1:magentoInfoResponse>   </env:Body> </env:Envelope>

The $magentoInfo variable is a MagentoStructMagentoInfoEntity object as:

MagentoStructMagentoInfoEntity Object (     [magento_version] =>     [magento_edition] => Community )

So you can easily display this information like this :

$version = $magentoInfo->getMagento_version(); $edition = $magentoInfo->getMagento_edition(); echo "Your Magento version is $version of the $edition edition";

Which should diplay: Your Magento version is of the Community edition.

Second service calls: store list and store info

We are going to call successively the storeList and the storeInfo operations. The second operation will be called using the result of the first one.

Get your store list:

$magentoServiceStore = new MagentoServiceStore($wsdl); $storeList = $magentoServiceStore->storeList($sessionId); 

The $storeList variable should be an array that contains MagentoStructStoreEntity objects as:

Array (     [0] => MagentoStructStoreEntity Object         (             [store_id] => 1             [code] => default             [website_id] => 1             [group_id] => 1             [name] => Default Store View             [sort_order] => 0             [is_active] => 1         ) ... etc. if you ave more than one store hosted on your magento website ) 

In comparison, the XML response looks like:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:enc="" xmlns:xsd="" xmlns:xsi=""> <env:Body xmlns:rpc=""> <ns1:storeListResponse env:encodingStyle=""> <rpc:result>stores</rpc:result> <stores enc:itemType="ns1:storeEntity" enc:arraySize="1" xsi:type="ns1:storeEntityArray"> <item xsi:type="ns1:storeEntity"> <store_id xsi:type="xsd:int">1</store_id> <code xsi:type="xsd:string">default</code> <website_id xsi:type="xsd:int">1</website_id> <group_id xsi:type="xsd:int">1</group_id> <name xsi:type="xsd:string">Default Store View</name> <sort_order xsi:type="xsd:int">0</sort_order> <is_active xsi:type="xsd:int">1</is_active> </item> </stores> </ns1:storeListResponse> </env:Body> </env:Envelope>

Now that we have at least one store returned, we can call the storeInfo operation to see what additional informations we can retrieve compared to those returned by the storeList operation.

$storeInfo = $magentoServiceStore->storeInfo($sessionId, new MagentoStructString($storeList[0]->getStore_id()));

This call uses the previous result as the $storeList array variable. When we look to the XML response, we can see that it returns the same informations that are also contained by a MagentoStructStoreEntity object:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:xsd="" xmlns:xsi="" xmlns:enc=""> <env:Body xmlns:rpc=""> <ns1:storeInfoResponse env:encodingStyle=""> <rpc:result>info</rpc:result> <info xsi:type="ns1:storeEntity"> <store_id xsi:type="xsd:int">1</store_id> <code xsi:type="xsd:string">default</code> <website_id xsi:type="xsd:int">1</website_id> <group_id xsi:type="xsd:int">1</group_id> <name xsi:type="xsd:string">Default Store View</name> <sort_order xsi:type="xsd:int">0</sort_order> <is_active xsi:type="xsd:int">1</is_active> </info> </ns1:storeInfoResponse> </env:Body> </env:Envelope>

So the PHP result, as the $storeInfo variable, is:

MagentoStructStoreEntity Object ( [store_id] => 1 [code] => default [website_id] => 1 [group_id] => 1 [name] => Default Store View [sort_order] => 0 [is_active] => 1 )

Third service calls: deal with your Magento catalog

Magento Web service provides a lot of operations to manage your website programmatically so you can easily import/export data from it. In our case, we are going to:

  1. Retrieve the category tree of the Magento website
  2. Retrieve the informations about a specific category
  3. Try to delete a specific category
  4. Create a product from scratch
  5. Retrieve the product list to validate the data associated to the product created previously

All these operations are going to use the Magento catalog operations. In order to do that, we initiate the service object dedicated to the catalog operations such as:

$magentoServiceCatalog = new MagentoServiceCatalog($wsdl);

From now, we'll always use the $magentoServiceCatalog object in order to call the operations to achieve the five steps enumerated previously.

Retrieve the category tree

Now that we have the caller initiated, we can easily call the Magento catalogCategoryTree operation like below:

// a little explanation about the : // - second parameter, new MagentoStructString(): it must contain the parentid of the category tree // - the third parameter, new MagentoStructString(): it must contain the storeView of the category. $catalogCategoryTree = $magentoServiceCatalog->catalogCategoryTree($sessionId, new MagentoStructString(), new MagentoStructString());

As we don't define any parentId an neither the storeView, we are going to get all the category tree.

For your Magento website, here is the XML response:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:xsd="" xmlns:xsi="" xmlns:enc=""> <env:Body xmlns:rpc=""> <ns1:catalogCategoryTreeResponse env:encodingStyle=""> <rpc:result>tree</rpc:result> <tree xsi:type="ns1:catalogCategoryTree"> <category_id xsi:type="xsd:int">1</category_id> <parent_id xsi:type="xsd:int">0</parent_id> <name xsi:type="xsd:string">Root Catalog</name> <position xsi:type="xsd:int">0</position> <level xsi:type="xsd:int">0</level> <children enc:itemType="ns1:catalogCategoryEntity" enc:arraySize="2" xsi:type="ns1:ArrayOfCatalogCategoryEntities"> <item xsi:type="ns1:catalogCategoryEntity"> <category_id xsi:type="xsd:int">2</category_id> <parent_id xsi:type="xsd:int">1</parent_id> <name xsi:type="xsd:string">Default Category</name> <is_active xsi:type="xsd:int">1</is_active> <position xsi:type="xsd:int">1</position> <level xsi:type="xsd:int">1</level> <children enc:itemType="ns1:catalogCategoryEntity" enc:arraySize="0" xsi:type="ns1:ArrayOfCatalogCategoryEntities"/> </item> <item xsi:type="ns1:catalogCategoryEntity"> <category_id xsi:type="xsd:int">3</category_id> <parent_id xsi:type="xsd:int">1</parent_id> <name xsi:type="xsd:string">Root</name> <is_active xsi:type="xsd:int">1</is_active> <position xsi:type="xsd:int">2</position> <level xsi:type="xsd:int">1</level> <children enc:itemType="ns1:catalogCategoryEntity" enc:arraySize="0" xsi:type="ns1:ArrayOfCatalogCategoryEntities"/> </item> </children> </tree> </ns1:catalogCategoryTreeResponse> </env:Body> </env:Envelope>

This XML resquest corresponds to a MagentoStructCatalogCategoryTree object as the $catalogCategoryTree variable:

MagentoStructCatalogCategoryTree Object ( [category_id] => 1 [parent_id] => 0 [name] => Root Catalog [position] => 0 [level] => 0 [children] => Array ( [0] => MagentoStructCatalogCategoryEntity Object ( [category_id] => 2 [parent_id] => 1 [name] => Default Category [is_active] => 1 [position] => 1 [level] => 1 [children] => Array ( ) ) [1] => MagentoStructCatalogCategoryEntity Object ( [category_id] => 3 [parent_id] => 1 [name] => Root [is_active] => 1 [position] => 2 [level] => 1 [children] => Array ( ) ) ) )

We have the Root category we created manually from the Backend contained by the default Magento Root Catalog category.

The Default Category is also one of the default Magento category.

Let's retrieve one category informations.

Retrieve a category info

As we retrieved the categories, we are going to use the previous result, $catalogCategoryTree, to get the Default Category information stored in Magento by calling the Magento catalogCategoryInfo operation:

// once again, a little explanation about the parameters: // - the second parameter: it must contain the id of the category // - the third parameter: it must contain the storeView value // - the fourth parameter: an optional array of attributes $categoryId = $catalogCategoryTree->children[0]->getCategory_id(); $catalogCategoryInfo = $magentoServiceCatalog->catalogCategoryInfo($sessionId, new MagentoStructInt($categoryId), new MagentoStructString(), null);

This call returns many informations about the category. The XML response looks like this:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:xsd="" xmlns:xsi="" xmlns:enc=""> <env:Body xmlns:rpc=""> <ns1:catalogCategoryInfoResponse env:encodingStyle=""> <rpc:result>info</rpc:result> <info xsi:type="ns1:catalogCategoryInfo"> <category_id xsi:type="xsd:string">2</category_id> <is_active xsi:type="xsd:int">1</is_active> <position xsi:type="xsd:string">1</position> <level xsi:type="xsd:string">1</level> <parent_id xsi:type="xsd:string">1</parent_id> <all_children xsi:type="xsd:string">2</all_children> <children xsi:type="xsd:string"/> <created_at xsi:type="xsd:string">2013-07-13 15:26:40</created_at> <updated_at xsi:type="xsd:string">2013-07-13 15:26:40</updated_at> <name xsi:type="xsd:string">Default Category</name> <url_key xsi:type="xsd:string">default-category</url_key> <path xsi:type="xsd:string">1/2</path> <url_path xsi:type="xsd:string">/default-category</url_path> <children_count xsi:type="xsd:int">0</children_count> <display_mode xsi:type="xsd:string">PRODUCTS</display_mode> <available_sort_by enc:itemType="xsd:string" enc:arraySize="0" xsi:type="ns1:ArrayOfString"/> <default_sort_by xsi:type="xsd:string">position</default_sort_by> </info> </ns1:catalogCategoryInfoResponse> </env:Body> </env:Envelope>

So the $catalogCategoryInfo variable is a MagentoStructCatalogCategoryInfo object that contains the informations of the XML response:

MagentoStructCatalogCategoryInfo Object ( [category_id] => 2 [is_active] => 1 [position] => 1 [level] => 1 [parent_id] => 1 [all_children] => 2 [children] => [created_at] => 2013-07-13 15:26:40 [updated_at] => 2013-07-13 15:26:40 [name] => Default Category [url_key] => default-category [description] => [meta_title] => [meta_keywords] => [meta_description] => [path] => 1/2 [url_path] => /default-category [children_count] => 0 [display_mode] => PRODUCTS [is_anchor] => [available_sort_by] => Array ( ) [custom_design] => [custom_design_apply] => [custom_design_from] => [custom_design_to] => [page_layout] => [custom_layout_update] => [default_sort_by] => position [landing_page] => )

Try to delete a category

The deletion of a category is made calling the Magento catalogCategoryDelete operation. Before doing this, you should ensure that you won't delete the wrong category. To validate the call, we would advice you to create a fake category from the backend and then try to delete it with the Magento web service.

In our case, we use the previous result, as the $catalogCategoryInfo variable, to try to delete a category from the catalog. We do like below:

$categoryId = $catalogCategoryInfo->getCategory_id(); $categoryDelete = $magentoServiceCatalog->catalogCategoryDelete($sessionId,new MagentoStructInt($categoryId));

In our case, we are trying to delete the Default Category, so we have this message in return:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env=""> <env:Body> <env:Fault> <env:Code> <env:Value>105</env:Value> </env:Code> <env:Reason> <env:Text>Can't delete root category.</env:Text> </env:Reason> </env:Fault> </env:Body> </env:Envelope>

Which is pretty explicit! If you want to get it programmatically in order to send emails or notifications or whatever, you can call the method $magentoServiceCatalog->getLastError() which returns an array containing the last errors with the method name as the array key and the SoapFault as the value. It gives something like this:

Array ( [MagentoServiceCatalog::catalogCategoryDelete] => SoapFault Object ( [message:protected] => Can't delete root category. [string:Exception:private] => [code:protected] => 0 [file:protected] => Magento/Catalog/MagentoServiceCatalog.php [line:protected] => 199 [trace:Exception:private] => Array ( [0] => Array ( [file] => Magento/Catalog/MagentoServiceCatalog.php [line] => 199 [function] => __call [class] => SoapClient [type] => -> [args] => Array ( [0] => catalogCategoryDelete [1] => Array ( [0] => MagentoStructString Object ( [_] => 32a7cab376ec550bad63b91e519ad11f [id] => [href] => ) [1] => MagentoStructInt Object ( [_] => 2 [id] => [href] => ) ) ) ) [1] => Array ( [file] => Magento/Catalog/MagentoServiceCatalog.php [line] => 199 [function] => catalogCategoryDelete [class] => SoapClient [type] => -> [args] => Array ( [0] => MagentoStructString Object ( [_] => 32a7cab376ec550bad63b91e519ad11f [id] => [href] => ) [1] => MagentoStructInt Object ( [_] => 2 [id] => [href] => ) ) ) [2] => Array ( [file] => magento.php [line] => 107 [function] => catalogCategoryDelete [class] => MagentoServiceCatalog [type] => -> [args] => Array ( [0] => MagentoStructString Object ( [_] => 32a7cab376ec550bad63b91e519ad11f [id] => [href] => ) [1] => MagentoStructInt Object ( [_] => 2 [id] => [href] => ) ) ) ) [previous:Exception:private] => [faultstring] => Can't delete root category. [faultcode] => 105 ) )

As you can see, the SoapFault contains the details on the error so you can manage it as you wish.

Create a product in a category

Creating products programmatically can be very convienent when you have to make data importation from an oustide product management tool. Using the Magento catalogProductCreate operation is very useful and is pretty easy following these steps

// Instantiate a new product entiry to create $productCreate = new MagentoStructCatalogProductCreateEntity(); // We use the previous category as the product parent category $productCreate->setCategories(array(new MagentoStructString($catalogCategoryInfo->getCategory_id()))); $productCreate->setCategory_ids(array(new MagentoStructString($catalogCategoryInfo->getCategory_id()))); // This design can be found using the backend web interface. You can also pass an empty string to let Magento use the default design. $productCreate->setCustom_design(new MagentoStructString('default/iphone')); $productCreate->setDescription(new MagentoStructString('My first product description')); $productCreate->setMeta_description(new MagentoStructString('My meta description')); $productCreate->setMeta_keyword(new MagentoStructString('magento,product,catalog')); $productCreate->setMeta_title(new MagentoStructString('My Magento product')); $productCreate->setName(new MagentoStructString('My first Magento product')); $productCreate->setPrice(new MagentoStructString(39.99)); $productCreate->setShort_description(new MagentoStructString('My first Magento product short description')); $productCreate->setStatus(new MagentoStructString(1)); $productCreate->setTax_class_id(new MagentoStructString(0)); $productCreate->setUrl_key(new MagentoStructString('my_first_magento_product')); $productCreate->setUrl_path(new MagentoStructString('/default-category/my-first-magento-product')); // The visibility value can be know by looking to the different values of the visibility values in your backend $productCreate->setVisibility(new MagentoStructString(4)); $productCreate->setWeight(new MagentoStructString(1.2)); // The product type we're creating $type = new MagentoStructString('simple'); // The product set $set = new MagentoStructString(4);  // The product SKU, it's a sample, define a good and reliable SKU of your convenience ;) $sku = new MagentoStructString('MAGENTO_PRODUCT_' . date('YmdHis')); // The store view $storeView = new MagentoStructString('default'); // Call the operation to create the product $catalogProductCreate = $magentoServiceCatalog->catalogProductCreate($sessionId,$type,$set,$sku,$productCreate,$storeView); 

It's always good to look at the XML request to visualize how the request is made:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:xsd="" xmlns:xsi="" xmlns:enc=""> <env:Body> <ns1:catalogProductCreate env:encodingStyle=""> <sessionId xsi:type="enc:string">813882c86fac883b573a077efe993137</sessionId> <type xsi:type="enc:string">simple</type> <set xsi:type="enc:string">4</set> <sku xsi:type="enc:string">MAGENTO_PRODUCT_20130718214656</sku> <productData xsi:type="ns1:catalogProductCreateEntity"> <categories enc:itemType="xsd:string" enc:arraySize="1" xsi:type="ns1:ArrayOfString"> <item xsi:type="enc:string">2</item> </categories> <name xsi:type="enc:string">My first Magento product</name> <description xsi:type="enc:string">My first product description</description> <short_description xsi:type="enc:string">My first Magento product short description</short_description> <weight xsi:type="enc:string">1.2</weight> <status xsi:type="enc:string">1</status> <url_key xsi:type="enc:string">my_first_magento_product</url_key> <url_path xsi:type="enc:string">/default-category/my-first-magento-product</url_path> <visibility xsi:type="enc:string">4</visibility> <category_ids enc:itemType="xsd:string" enc:arraySize="1" xsi:type="ns1:ArrayOfString"> <item xsi:type="enc:string">2</item> </category_ids> <price xsi:type="enc:string">39.99</price> <tax_class_id xsi:type="enc:string">0</tax_class_id> <meta_title xsi:type="enc:string">My Magento product</meta_title> <meta_keyword xsi:type="enc:string">magento,product,catalog</meta_keyword> <meta_description xsi:type="enc:string">My meta description</meta_description> <custom_design xsi:type="enc:string">default/iphone</custom_design> </productData> <storeView xsi:type="enc:string">default</storeView> </ns1:catalogProductCreate> </env:Body> </env:Envelope>

And so the XML response is:

<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="" xmlns:ns1="urn:Magento" xmlns:xsd="" xmlns:xsi="" xmlns:enc=""> <env:Body xmlns:rpc=""> <ns1:catalogProductCreateResponse env:encodingStyle=""> <rpc:result>result</rpc:result> <result xsi:type="xsd:int">27</result> </ns1:catalogProductCreateResponse> </env:Body> </env:Envelope>

For an unknown reason, this response generates an error: PHP Fatal error:  SOAP-ERROR: Encoding: Violation of encoding rules, so it's blocking us to get the response as a PHP object. In the meantime, if you wish to retrieve the product id you've just created that is contained in the XML response, do as below:

$productId = $magentoServiceCatalog->getLastResponse(true)->getElementsByTagName('result')->item(1)->nodeValue;

In our example, the $productId variable is equal to 27.

Export your product list

As we just created a product, it would be great to see how its informations are returned among the product list using the Magento catalogProductList operation. Moreover, exporting the product list from magento can be useful to export them as a CSV file or a XML file or to send them to tierce marketing management tool. To get the product list, do as below:

$catalogProductList = $magentoServiceCatalog->catalogProductList($sessionId,new MagentoStructFilters(),new MagentoStructString('default'));

In this case, you can get a really big XML response and so the PHP response. The PHP response is an array of MagentoStructCatalogProductEntity object and looks like:

Array ( [0] => MagentoStructCatalogProductEntity Object ( [product_id] => 1 [sku] => MAGENTO_PRODUCT_20130713192402 [name] => My first Magento product [set] => 4 [type] => simple [category_ids] => Array ( [0] => 2 ) [website_ids] => Array ( [0] => 1 ) ) [1] => MagentoStructCatalogProductEntity Object ( [product_id] => 2 [sku] => MAGENTO_PRODUCT_20130713193233 [name] => My first Magento product [set] => 4 [type] => simple [category_ids] => Array ( [0] => 2 ) [website_ids] => Array ( [0] => 1 ) ) etc. )

As you can see, you can easily iterate through results and then manipulate each product as they are PHP objects.

Third service calls: manage your Magento customers

One another big thing in e-commerce is the customers. They are often managed externally by marketing tools and often with CRM tools for marketing objectives. So you will maybe wish to export them from the Magento website but you could also need to create them in your Magento website in the case you're migrating to Magento from another e-commerce solution. Doing it manually would be really fastidious so why don't do it programmatically. Once again, it is very powerful to do it programmatically as you can do it as mush time as you need once you've created the mapping process from your old e-commerce solution to your new Magento website.

We are going to use the Magento customer operations. In order to do that, we initiate the service object dedicated to the customer operations such as:

$magentoServiceCustomer = new MagentoServiceCustomer($wsdl);

From now, we'll always use the $magentoServiceCustomer object in order to call the operations.

Create a customer

To do so, we use the Magento customerCustomerCreate operation. It's pretty the same procedure as the product create operation but for customer:

// Instantiate a customer entity to create $customer = new MagentoStructCustomerCustomerEntityToCreate(); $customer->setEmail(new MagentoStructString('contact+' . time() . '')); $customer->setFirstname(new MagentoStructString('Mikaël')); $customer->setGroup_id(new MagentoStructInt(1)); $customer->setLastname(new MagentoStructString('DELSOL')); $customer->setPassword(new MagentoStructString('mikael-delsol')); // Once agin, we use the store list retrieved previously $customer->setStore_id(new MagentoStructString($storeList[0]->getStore_id())); // Call the create operation $customerCustomerCreate = $magentoServiceCustomer->customerCustomerCreate($sessionId,$customer);

As for the product create operation, an error is triggered so you'll have to use the same trick to get the new customer id:

$customerId = $magentoServiceCustomer->getLastResponse(true)->getElementsByTagName('result')->item(1)->nodeValue;

Add an address to a customer

We created a customer but he is pretty basic and does not hold any delivery address neither any billing address, so let's do it! To add an address to a customer, use the Magento customerAddressCreate operation:

// Instantiate a new address entity to create $address = new MagentoStructCustomerAddressEntityCreate(); $address->setCity(new MagentoStructString('115 Crm Firms')); $address->setCompany(new MagentoStructString('WSDL to php')); $address->setCountry_id(new MagentoStructString('US')); $address->setFirstname(new MagentoStructString('Mikaël')); $address->setIs_default_billing(new MagentoStructBoolean(true)); $address->setIs_default_shipping(new MagentoStructBoolean(true)); $address->setLastname(new MagentoStructstring('DELSOL')); $address->setMiddlename(new MagentoStructstring('John')); $address->setPostcode(new MagentoStructstring(11597)); $address->setStreet(new MagentoStructstring('10 John Street')); $address->setTelephone(new MagentoStructstring('000 000 000')); // Call the customerAddressCreate operation. // The $customerId comes from the XML response of the customer created // in or attach this new address to him. $customerAddressCreate = $magentoServiceCustomer->customerAddressCreate($sessionId,new MagentoStructInt($customerId),$address);

As for the product creation and the customer creation you get the customer address you just created.


As we demonstrated, using the Magento SOAP API is not very hard and can really ease the automation of data manipulation prorgammatically. We can easily imagine to create  automated tasks that import or export data from or to the Magento website. This SOAP API is pretty complete and useful!

You need help or encounter issue on calling another operation, feel free to ask, we're here to help you out ;).

Anyway, we really hope it will help you handle the Magento SOAP API to fully master you e-commerce website.

This post has 1 comment:

The August 03, 2016 at 03:59:11, john wrote:

Its very useful! Thanks for share

Total 1 comments