Tuesday 5 March 2013

Magento API / web service work

Magento has strong web service features. I can say Magento’s web service is one step ahead than others e-commerce. Magneto has soap, v2_soap(soap 2) and xmlrpc adapter facilities. Using magento web services you can synchronize customer, categories, products, orders etc data with existing stores. Here I am going to describe magento’s web service deals step by step.
Setup API: At first create api user and api key from Magento’s admin. Follow below steps.
1. Create api user from admin->system->web services-> Users->Add New User->User Info. See picture.
Api user create with api key
Api user create with api key
2. Create role to set permission on api method under admin->system->web services-> Roles->Add New Role.
3. Define “Role Resources” for created role. See picture.
Set api permission on role
Set api permission on role
4. Set “User Role” from admin->system->web services-> Users->choose user->User Role.
Call API:
API user and key is created. Now we can call api methods in two ways.
1. Soap call
2. V2_Soap call
Here I am going to discuss both ways.
Soap call:
Magento wiki and help center have described this method. So who are beginner in web service work they can call magento api using this soap method. You get all api documentations in Magento site. Here is an example.
01//create soap object
02$proxy = new SoapClient('http://127.0.0.1/magento/api/soap/?wsdl');
03 
04// create authorized session id using api user name and api key
05// $sessionId = $proxy->login('apiUser', 'apiKey');
06$sessionId = $proxy->login('m4u_admin', '12345678');
07 
08 // Get customer info for customer id = 1
09$customerinfo = $proxy->call($sessionId, 'customer.info', 1);
10 
11print_r($customerinfo);

V2_Soap call:

V2_soap call is for advance user. Who are expert in web service work they can easily use this method to call any magento api method from remote place. Whose magento knowledge is zero they also can use this soap method to work with magento web service. But about this method you will not get any api documentation in Magento wiki. But I think it will easy for you.
Here I am going to describe a-z about v2_soap method as any beginner also can use this soap calling method.
01//create soap object
02$proxy = new SoapClient('http://127.0.0.1/magento/api/v2_soap/?wsdl');
03 
04// create authorized session id using api user name and api key
05// $sessionId = $proxy->login('apiUser', 'apiKey');
06$sessionId = $proxy->login('m4u_admin', '12345678');
07 
08 // Get customer info for customer id = 1
09$customerinfo = $proxy->customerCustomerInfo($sessionId,1);
10 
11print_r($customerinfo);
Which method you need to call?
At first decide which method you need to call of api. Suppose you want to retrieve customer info from Magento store. So you should call customer related api method to get customer info. But you donot know which method exactly you need. To view all available methods in your magento api please run http://127.0.0.1/magento/api/v2_soap/?wsdl. Now pick appropriate api method from wsdl operation list. Suppose I pick “customerCustomerInfo” method to get customer info.
1<operation name="customerCustomerInfo">
2   <documentation>Retrieve customer data</documentation>
3   <input message="typens:customerCustomerInfoRequest"/>
4   <output message="typens:customerCustomerInfoResponse"/>
5</operation>
What is request parameter?
From wsdl you get a clear concept about api request/input parameter. For “customerCustomerInfo” operation input message is “customerCustomerInfoRequest”. So you will get all input/request parameter info (name and type) to call customerCustomerInfo method in message “customerCustomerInfoRequest”. customerCustomerInfo has three request parameter such sessionid(string), customerid(int) and atribuates(Array).
1<message name="customerCustomerInfoRequest">
2    <part name="sessionId" type="xsd:string"/>
3    <part name="customerId" type="xsd:int"/>
4    <part name="attributes" type="typens:ArrayOfString"/>
5</message>  
So we can request customerCustomerInfo method as below.
// Get customer info for customer id = 1 and attribute is optional
$attribute = array(‘firstname’, ‘lastname’, ‘email’, ‘store_id’);
$customerinfo = $proxy->call($sessionId, ‘customer.info’, 1, $attribute);
see picture for details-

API v2_soap wsdl request and response message.
API v2_soap wsdl request and response message.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home