Tuesday 5 March 2013

Multi Web service calls

Supported types

The Magneto Core API supports SOAP and XML-RPC, where SOAP is the default protocol.


To connect to Magento SOAP web services, load theWSDL into your SOAP client from either of these URLs:
  • http://mymagentohost/api/?wsdl
  • http://mymagentohost/api/soap/?wsdl
where mymagentohost is the domain for your Magneto host.
As of v1.3, you may also use the following URL to access the Magento API v2, which has been added to improve compatibility with Java and .NET:
  • http://mymagentohost/api/v2_soap?wsdl=1
The following PHP example shows how to make SOAP calls to the Magento API v1:
  1. $client = new SoapClient('http://mymagentohost/api/soap?wsdl');
  3. // If somestuff requires api authentification,
  4. // then get a session token
  5. $session = $client->login('apiUser', 'apiKey');
  7. $result = $client->call($session, 'somestuff.method');
  8. $result = $client->call($session, 'somestuff.method', 'arg1');
  9. $result = $client->call($session, 'somestuff.method', array('arg1', 'arg2', 'arg3'));
  10. $result = $client->multiCall($session, array(
  11.      array('somestuff.method'),
  12.      array('somestuff.method', 'arg1'),
  13.      array('somestuff.method', array('arg1', 'arg2'))
  14. ));
  17. // If you don't need the session anymore
  18. $client->endSession($session);


To use XML-RPC, load the following URL into your XML-RPC client:
  • http://mymagentohost/api/xmlrpc/
where mymagentohost is the domain for your Magneto host.
The following PHP example shows how to make XML-RPC calls:
  1. $client = new Zend_XmlRpc_Client('http://youmagentohost/api/xmlrpc/');
  3. // If somestuff requires api authentification,
  4. // we should get session token
  5. $session = $client->call('login', array('apiUser', 'apiKey'));
  7. $client->call('call', array($session, 'somestuff.method', array('arg1', 'arg2', 'arg3')));
  8. $client->call('call', array($session, 'somestuff.method', 'arg1'));
  9. $client->call('call', array($session, 'somestuff.method'));
  10. $client->call('multiCall', array($session,
  11.      array(
  12.         array('somestuff.method', 'arg1'),
  13.         array('somestuff.method', array('arg1', 'arg2')),
  14.         array('somestuff.method')
  15.      )
  16. ));
  18. // If you don't need the session anymore
  19. $client->call('endSession', array($session));
The XML-RPC only supports the version 1 of the Magento API.

API Methods

The following table contains the API methods that can be called from your SOAP or XML-RPC client on the Magento v1 API.
Method Description Return Value
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 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 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 current session. array
Note: For call and multiCall, if no session is specified, you can call only resources that are not protected by ACL.
Note: For multiCall, if the “break” option is specified, multiCall breaks on first error.
The Magento API v2 does not support the call() and multiCall() methods, and instead provides a separate method for each API resource.

Global API Faults

The following table contains fault codes that apply to all API calls.
Fault Code Fault Message
0 Unknown Error
1 Internal Error. Please see log for details.
2 Access denied.
3 Invalid api path.
4 Resource path is not callable.

API Version v2

Since Magento 1.3, version v2 of the API has also been available. The main difference between v1 and v2 is that instead of using methods call and multiCall, it has separate methods for each action.
For example, consider the following PHP code using v1.
  1. $params = array(array(
  2.     'status'=>array('eq'=>'pending'),
  3.     'customer_is_guest'=>array('eq'=>'1'))
  4. ));
  5. $result = $client->call($sessionId, 'sales_order.list', $params);
With v2, the following code would be equivalent.
  1. $params = array('filter' => array(
  2.     array('key' => 'status', 'value' => 'pending'),
  3.     array('key' => 'customer_is_guest', 'value' => '1')
  4. ));
  5. $result = $client->salesOrderList($sessionId, $params);
Note that the WSDL for v1 and v2 are different. Note that in v1, customizing the API did not involve changing the WSDL. In v2, changes to the WSDL are required.
You can configure the v2 API to be WS-I compliant in the system configuration menu. To do this, set Services > Magento Core API > WS-I Compliance to Yes.
Note that the WSDL for the v2 API is different when in WS-I compliant mode.
Using the WS-I compliant v2 API WSDL, it is easy to automatically generate client classes for Java, .NET, and other languages using standard libraries.

Source: http://www.magentocommerce.com/wiki/doc/webservices-api/api


Post a Comment

Subscribe to Post Comments [Atom]

<< Home