Magentos API provides you with the ability to manage your eCommerce stores by providing calls for working with data such as customers, categories, products, and sales orders. It also allows you to manage shopping carts and inventory.
You could use one of two methods Rest or SOAP. In this article I’m going to focus on the SOAP route using PHP’s native SoapClient. I’ll explain how to connect using the SOAP API and provide you with a few simple examples of it’s potential uses.
First of all
In Magento create a user and assign a role under the Configuration > Web Services > Soap Users/Soap Roles. Once you have done this your store is ready to accept connections from this user. It may be worth while keeping an eye on the error log on the Magento store if any errors do occur as the third party application does not.
You will need to install soap on the server that the 3rd party application is sitting on.
$host = ‘http://magento/api/?msdl’; $username = ‘foo’; $api = ‘ig87634sd’ $client = new SoapClient( $host); $session = $client->login($username, $api);
The session is used on every call you make to the SOAP API so it may be worth while storing this in a variable instead of authenticating for every call.
Updating Product Stock Example
$prod = new stdClass(); $prod->qty = 2; $product_id = 2; $qty = 23; $client->updateProductStock($session, $product_id, $prod);
Getting Products Example
$params = array(‘filter’ => array( array(‘key’ => ‘type_id’, ‘value’ => ‘virtual’) )); $client->catalogProductList($session, $params);
Getting Complete Orders Example
$params = array(‘filter’ => array( array(‘key’ => ‘state’, ‘value’ => ‘complete’) )); $client->salesOrderInfo($session, $params);
Other Methods Available
|startSession()||Start the API session and return session ID||string|
|endSession(sessionId)||End the API session.||boolean|
|login(apiUser, apiKey)||Start the API session, return the session ID, and authorize the API user||string|
|call(sessionId, resourcePath,array arguments)||Call the API resource that is allowed in the current session. See Note below.||mixed|
|multiCall(sessionId, array calls,array options)||Call the API resource’s methods that are allowed for current session. See Notes below.||array|
|resources(sessionId)||Return a list of available API resources and methods allowed for the current session||array|
|globalFaults(sessionId)||Return a list of fault messages and their codes that do not depend on any resource.||array|
|resourceFaults(sessionId, resourceName)||Return a list of the specified resource fault messages, if this resource is allowed in the current session||array|
SOAP is a good method of communicating with Magento. It has its restrictions regarding limiting the result set, but it can be expanded by a custom module if you so wish to add extra functionality to the API.