Dear Magento friends,

Last time, we have started with Widgets in the last topic of Advanced Features. Have you enjoyed it? This week, we will continue with API functions.

You may probably know that API is one of the most important things to developers and, Magento provides API functions to allow developers to work with Magento from a third party system. To explore it in more details, why not start reading right now?

1. Magento Core API

On version 1.5, Magento API supports two API protocols: SOAP and XML-RPC, of which SOAP is the default.

To connect with Magento SOAP web services, you can use these links for your SOAP client:

http://magentosite/index.php/api/soap/?wsdl
http://magentosite/index.php/api/?wsdl

This is an example of SOAP client code written in PHP:

$client = new SoapClient('http://magentosite/index.php/api/soap/?wsdl');

// get sessionId before call Magento Core API function
$sessionId = $client->login('apiUser', 'apiKey');

$result = $client->call($sessionId, 'order.list');
$result = $client->call($sessionId, '.', 'arg1');
$result = $client->call($sessionId, '.', array('arg1', 'arg2', 'arg3'));
$result = $client->multiCall($sessionId, array(
array('.'),
array('.', 'arg1'),
array('.', array('arg1', 'arg2'))
));

// If you don't need the session anymore
$client->endSession($sessionId);

To connect with Magento XML-RPC web services, you need to use this link for your XML-RPC client:

http://magentosite/index.php/api/xmlrpc/

Please look at an example code using XML-RPC client:

$client = new Zend_XmlRpc_Client('http://magentosite/index.php/api/xmlrpc/');

// get sessionId before call Magento Core API function
$sessionId = $client->call('login', array('username' => 'apiUser', 'password' => 'apiKey'));

$result = $client->call('call', array($sessionId, '.'));
$result = $client->call('call', array($sessionId, '.', 'arg1'));
$result = $client->call('call', array($sessionId, '.', array('arg1', 'arg2', 'arg3')));
$result = $client->call('multiCall', array($sessionId, array(
array('.'),
array('.', 'arg1'),
array('.', array('arg1', 'arg2'))
)));

// If you don't need the session anymore
$client->call('endSession', array($sessionId));

Magento Core API allows you to manage customers, categories, products, sales, checkout process, countries and regions. For the full list of core API methods, we can visit . In that post, I have an example using Magento API method customer.info, this code below:

$client = new Zend_XmlRpc_Client('http://magentohost/api/xmlrpc/');

// get sessionId before call Magento Core API function
$sessionId = $client->call('login', array('username' => 'apiUser', 'password' => 'apiKey'));

$result = $client->call('call', array($sessionId, 'customer.info', '1'));

print_r($result);

// If you don't need the session anymore
$client->call('endSession', array($sessionId));

And then, the result will be:

Array
(
[customer_id] => 1
[created_at] => 2007-08-30 23:23:13
[updated_at] => 2008-08-08 12:28:24
[increment_id] => 000000001
[store_id] => 1
[website_id] => 1
[firstname] => John
[lastname] => Doe
[email] => john.doe@example.com
[password_hash] => 2049484a4020ed15d0e4238db22977d5:eg
[default_billing] => 274
[default_shipping] => 274
[created_in] =>
[group_id] => 1
[prefix] =>
[middlename] =>
[suffix] =>
[dob] =>
[taxvat] =>
[confirmation] =>
[gender] =>
)

2. Create your own API

You may know how to use Magento API, but when you develop a Magento module, you may do not know how to add your custom API method for working on Magento. This topic will help you to implement that.

First of all, you need to declare your API method with system by adding code in file api.xml on etc folder of your module. This file has similar code to:

<!--?xml version="1.0"?--></p>
&lt;[custom_resource] translate="title" module="[custom_module]"&gt;

[custom_resource]
[custom_module]/[your_model]

&lt;[method] translate="title" module="[custom_module]"&gt;

[method]
[custom_resource]/[method]
<!--&#91;method&#93;-->

<!-- Example a fault response -->

<code>100</code>
Invalid data. View details in error message

<!--&#91;custom_resource&#93;-->

&lt;[custom_resource] translate="title" module="[custom_module]"&gt;

2000
&lt;[method] translate="title" module="[custom_module]"&gt;

<!--&#91;method&#93;-->
<!--&#91;custom_resource&#93;-->

Then you need to declare your module model in config.xml file and write your model as an adapter for API method above:

<!--?php</p--></p>
class [Your_Module]_Model_[your_model] extends Mage_Api_Model_Resource_Abstract
{
public function [method]() {
// put adapter code here
try {
...
} catch (Exception $e) {
// fault with the code tag data_invalid
$this-&gt;_fault('data_invalid', $e-&gt;getMessage());
}
}
}

Hope this is helpful for you to understand the Magento API.

Have a good time and see you again soon!

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

Write A Comment