The Magento SOAP v1 API provides you with the ability to manage your eCommerce stores by providing calls for working with resources such as customers, categories, products, and sales orders. It also allows you to manage shopping carts and inventory. Gives you ability to manage customers, categories, products, sales and checkout process.
When to use Magento API?
• You want to integrate with third party application.
• Why use Magento API?
• More stable than underlying code
• Less code implementation
• Tons of examples and documentation around API
What is SOAP?
SOAP is a XML-based protocol that allows applications to send messages in a standarized way over the Internet. The communication with SOAP boils down to a SOAP server and a SOAP client. Magento offers a SOAP server that allows you to communicate with Magento on a level that allows you to integrate Magento into your own application.
The benefit of using SOAP (or its rival XML-RPC) is that first of the communication is done through HTTP, so you don’t need any extra holes in the firewall. Also there are numerous SOAP-clients and XML-RPC-clients out there, and with most of them you don’t have to worry how the actual communication between your client and the Magento server is handled: The only thing you have to worry about is calling the right server-side API-method, which will return some kind of result, and knowing how to handle this result.
A: Create a Magento API user:
4)_In the panel on the left, select Role Resources. Then do one of the following:
– To enable full access to customer, catalog, and sales information, set Resource Access to “All.”
– To provide limited access to data, set Resource Access to “Custom.” Then, tick the “Sales”.
– checkbox from the list that appears below.
5) When complete, click the Save Role button.
2) Click the Add New User button.
3) Complete the following fields: User Name, First Name, Last Name, Email.
4) In the API Key field, enter a password/token of your choice. This will be used to when linking your account.
5) To confirm, re-enter the key in the API Key Confirmation field.
6) In the panel on the left, select User Role. Then, select the “API” Role created earlier.
7) When complete, click the Save User button.
Now we got magento API username and API Key, which is used to connect Magento with other application.
Example: Logging with the SOAP client
So let’s create a simple PHP-script that allows us to login into Magento through SOAP. The logic is here that we first need to initialize a new SoapClient object with as argument the Magento SOAP URL.
Magento login information
$mage_url = ‘http://MAGENTO/api/?wsdl’;
$mage_user = ‘soap_user’;
$mage_api_key = ‘********’;
Initialize the SOAP client
$soap = new SoapClient( $mage_url );
Login to Magento
$session_id = $soap->login( $mage_user, $mage_api_key );
After we have initialized the $soap-object we login with the API user-credentials created earlier. This will return a session-ID which we will need to reuse when calling other API-methods. The login() method is actually not part of the SoapClient-class. The class transforms this method-call into another call to the magic method __soapCall() with the first argument set to “login” and as second argument an array of the arguments given to the original method “login()” (in this case the user credentials).
This structure allows us to easily call any SOAP-method:
$response = $soap->getSomethingSpecific($arguments);
Example: A simple API-call
The listing shows us the various API-resources that can be used. These resources do not represent SOAP-methods themselves, but they can be fetched only through the SOAP-method “call()”. For instance the list of resources mentions the Country Api that has a method to fetch a list of countries. This method has a path “directory_country.list” and an alias “country.list”. To fetch this list of countries, we can use either the path or one the aliases:
$result = $soap->call( $session_id, 'directory_country.list' );
$result = $soap->call( $session_id, 'country.list' );
Again the result is a large array.
The Product API allows us to specify a product through its database ID or its SKU:
$product = $soap->call( $session_id, 'catalog_product.info', 166 );
$product = $soap->call( $session_id, 'catalog_product.info', 'HTC Touch Diamond' );
For instance if the Product ID is specified in the URL, you should prevent for exploits by making sure it is a number:
$product_id = $quoteId = $this->getRequest()->getParam('product_id', 265);
$product = $soap->call( $session_id, 'catalog_product.info', $product_id );
Giving multiple arguments is just as easy as giving one argument. The clue here is that if we want to pass a single argument to the API-call we can just add that single argument, but if we want to add multiple arguments we need an array:
$product = $soap->call( $session_id, 'catalog_product.info', array( 166 ));
Example: Changing data through the API
Now we can setup a SOAP client and use it retreive data from the Magento API. Changing data is done in exactly the same way as fetching data. The difference is that the arguments now hold not only the indicator “what we want to change” but also the data that contains the change:
$product_id = 166;
$product_data = array( 'status' => 2 );
$arguments = array( $product_id, $product_data );
$boolean = $soap->call( $session_id, 'catalog_product.update', $arguments);
Example: Using lists and filters
If we want to collect a list of products the Product API gives us the “list()” method:
<em>$products = $soap->call( $session_id, 'catalog_product.list' );</em>
This API-method has an extra argument $filters which allows for very cool possibilities. For instance the following adds a filter to show only products with the status set to 1, in other words products which are enabled:
$filters = array( 'status' => 1 );
$products = $soap->call( $session_id, 'catalog_product.list', array($filters) );
But the $filters can be made more advanced as well. The following filters select all enabled products that are of type “simple” (so neither configurable products nor grouped products) which have a SKU starting with the letters “ma”.
$filters = array(
'status' => array( '=' => 1 ),
'type_id' => array( '=' => 'simple' ),
'sku' => array( 'like' => 'ma%' ),
Example: Making multiple calls at once
With the available “list()” methods you can succesfully collect a list of items (products, categories, customers), but further inspection of the returned data will show that it does not include the full item-data. For instance when returning products the “description” is missing.
The logic is here that you can use “list()” to give a full-list of items (or for instance a list based upon filters) but that you will still need the “info()” method to get the full information for every item in that list.
Instead of making a “category_product.info” call for every product in the list (which will cost a lot of extra HTTP-traffic) you can combine multiple call in one SOAP-call through the “multiCall” method.
A simple example below:
$calls = array(
array( 'catalog_product.info', 166 ),
array( 'catalog_product.info', 167 ),
array( 'catalog_product.info', 168 ),
$results = $soap->multiCall( $session_id, $calls );
You can also combine multiple API-methods of different types in the same call:
$calls = array(
array( 'catalog_product.list' ),
array( 'customer.list' ),
array( 'directory_country.list' ),
$results = $soap->multiCall( $session_id, $calls );
Hope you get me !