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.
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.
4. Set “User Role” from admin->system->web services-> Users->choose user->User Role.
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.
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.
//create soap object $proxy = new SoapClient('http://127.0.0.1/magento/api/soap/?wsdl'); // create authorized session id using api user name and api key // $sessionId = $proxy->login('apiUser', 'apiKey'); $sessionId = $proxy->login('m4u_admin', '12345678'); // Get customer info for customer id = 1 $customerinfo = $proxy->call($sessionId, 'customer.info', 1); print_r($customerinfo);
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.
//create soap object $proxy = new SoapClient('http://127.0.0.1/magento/api/v2_soap/?wsdl'); // create authorized session id using api user name and api key // $sessionId = $proxy->login('apiUser', 'apiKey'); $sessionId = $proxy->login('m4u_admin', '12345678'); // Get customer info for customer id = 1 $customerinfo = $proxy->customerCustomerInfo($sessionId,1); print_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.
<operation name="customerCustomerInfo"> <documentation>Retrieve customer data</documentation> <input message="typens:customerCustomerInfoRequest"/> <output message="typens:customerCustomerInfoResponse"/> </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).
<message name="customerCustomerInfoRequest"> <part name="sessionId" type="xsd:string"/> <part name="customerId" type="xsd:int"/> <part name="attributes" type="typens:ArrayOfString"/> </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-
What is return/response data?
From wsdl you also get idea about api response/return/output. For “customerCustomerInfo” operation output message is “customerCustomerInfoResponse”. So you will get output/result info (name and type) of customerCustomerInfo method in message “customerCustomerInfoResponse”. customerCustomerInfo will return customerCustomerEntity object as data. Please see what the format of customerCustomerEntity object in wsdl.
<message name="customerCustomerInfoResponse"> <part name="customerInfo" type="typens:customerCustomerEntity"/> </message>