
MAGENTO 1.X – Create a SOAP API from scratch
Sometimes, you may need to expose your resources in different platforms making them platform independent. This could be possible thanks to protocols like XML-RPC/SOAP.
In this case, we are going to extend Magento standard SOAP module, in order to create a simple custom service.
To do so, we will need to perform the following steps.
First, we need to set up our local environment.
- Enable soap PHP extension.
12sudo apt-get install php5.6-soap # For PHP 5.6sudo apt-get install php-soap # For your current PHP - Enable SOAP extension on your php.ini file.
1extension=php_soap.dll - Restart apache
1sudo service apache2 restart - In your php info, you should now be able to see availble SOAP section, such as:
Now, we are available to start our work on the Magento side.
Let’s create a new local extension.
- We will need to create the following directories with its files inside:
1234567app/etc/modules/Vendor_Customext.xmlapp/code/local/Vendor/Customext/etc/api.xmlapp/code/local/Vendor/Customext/etc/config.xmlapp/code/local/Vendor/Customext/etc/wsdl.xmlapp/code/local/Vendor/Customext/Helper/Data.phpapp/code/local/Vendor/Customext/Model/Product/Api.phpapp/code/local/Vendor/Customext/Model/Product/v2.php - app/etc/modules/Vendor_Customext.xml
123456789<?xml version="1.0"?><config><modules><Vendor_Customext><active>true</active><codePool>local</codePool></Vendor_Customext></modules></config> - app/code/local/Vendor/Customext/etc/api.xml
12345678910111213141516171819202122232425262728293031323334353637<?xml version="1.0"?><config><api><resources><customext_order translate="title" module="customext"><model>customext/product_api</model><title>Demo Custom Module API</title><acl>customext/product</acl><methods><process translate="title" module="customext"><title>List of products</title><method>items</method></process></methods></customext_order></resources><resources_alias><product>customext_order</product></resources_alias><v2><resources_function_prefix><product>customextProduct</product></resources_function_prefix></v2><acl><resources><customext translate="title" module="customext"><title>Products</title><sort_order>5</sort_order><product translate="title" module="customext"><title>Product info</title></product></customext></resources></acl></api></config> - app/code/local/Vendor/Customext/etc/config.xml
1234567891011121314151617181920<?xml version="1.0"?><config><modules><Vendor_Customext><version>1.0</version></Vendor_Customext></modules><global><models><customext><class>Vendor_Customext_Model</class></customext></models><helpers><customext><class>Vendor_Customext_Helper</class></customext></helpers></global></config> - app/code/local/Vendor/Customext/etc/wsdl.xml
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253<?xml version="1.0" encoding="UTF-8"?><definitions xmlns:typens="urn:{{var wsdl.name}}" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"name="{{var wsdl.name}}" targetNamespace="urn:{{var wsdl.name}}"><types><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Magento"><import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" /><complexType name="fieldInfo"><sequence><element name="entity_id" type="xsd:string"/><element name="name" type="xsd:string"/></sequence></complexType><complexType name="fieldInfoArray"><complexContent><restriction base="soapenc:Array"><attribute ref="soapenc:arrayType" wsdl:arrayType="typens:fieldInfo[]" /></restriction></complexContent></complexType></schema></types><message name="customextProductListRequest"><part name="sessionId" type="xsd:string" /></message><message name="customextProductListResponse"><part name="products" type="typens:fieldInfoArray" /></message><portType name="{{var wsdl.handler}}PortType"><operation name="customextProductList"><documentation>List of products</documentation><input message="typens:customextProductListRequest" /><output message="typens:customextProductListResponse" /></operation></portType><binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /><operation name="customextProductList"><soap:operation soapAction="urn:{{var wsdl.handler}}Action" /><input><soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></input><output><soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /></output></operation></binding><service name="{{var wsdl.name}}Service"><port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding"><soap:address location="{{var wsdl.url}}" /></port></service></definitions>
This file is used to define the API method definitions as per the SOAP syntax.
– “fieldInfo” complex type, contains two elements “entity_id” and “name”.
– “fieldInfoArray” complex type, which is derived from the “fieldInfo” complex type. It’s an array of the “fieldInfo” complex type.
We’ve defined the object properties which will be returned in the response of the API method call.
In our case, we’ll return an array of products. Each item of an array will have two properties: “entity_id” and “name” of the product. You can define more properties as per your requirements.
Then, under the <message> tag “customextProductListRequest”, we’ve defined the input parameters required using the <part> tag.
In the same way, under the <message> tag “customextProductListResponse”, we’ve defined the type of the output objects.
When we call an API method, we need to pass “sessionId”, and the response of the API method will contain the array of products.
- app/code/local/Vendor/Customext/Helper/Data.php
123<?phpclass Vendor_Customext_Helper_Data extends Mage_Core_Helper_Abstract {} - app/code/local/Vendor/Customext/Model/Product/Api.php
12345678910111213141516171819<?phpclass Vendor_Customext_Model_Product_Api extends Mage_Api_Model_Resource_Abstract{public function items($param1, $param2){$arr_products=array();$products=Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*')->setOrder('entity_id', 'DESC')->setPageSize(5);foreach ($products as $product) {$arr_products = array('entity_id' => $product['entity_id'].$param1, 'name' => $product['name'].$param2);}return $arr_products;}}
Method “items” is where we are going to implement all our internal logic with our Magento resources, in order to give a response to the consumer of our service.
- app/code/local/Vendor/Customext/Model/Product/v2.php
123<?phpclass Vendor_Customext_Model_Product_Api_V2 extends Vendor_Customext_Model_Product_Api {}
Right now, we have our service extension finished, so we will need to configure our Magento backend in order to expose the API. Remember after writing the previuos code, cleaning the cache.
- Set up “api role”.
To do so, you need to login to your backend and go to System > Web Services > (SOAP/XMLRPC) Roles.
Right there, on the Role info tab, set a name for your role (eg: Custom Ext Role).Next, under the Role Resources tab, you will be able to check all available resources with check boxes. Click on the Products > Product info check-box. That is “Custom Ext Role” can only access the “Product info” resource APIs. Click on the Save Role button to save the role information.
- Set up “api user”.
Here you need to go under System > Web Services > (SOAP/XMLRPC) Users, and click on add new user button. Under user info tab, fill in all required fields. Then under User Role tab select your previous created role (eg: Custom Ext Role).
Now, we are available to consume our SOAP Resource, in order to do so, we will create the following PHP code.
1 2 3 4 5 6 7 8 |
<?php require_once(dirname(__FILE__) . '/app/Mage.php'); Mage::init(); $client = new SoapClient('http://local.myapphost.com/api/soap/?wsdl=1'); $session = $client->login('soapuser', 'soapuser'); $result = $client->call($session, 'pagosapi_order.list',array('Param 1', 'Param 2')); var_dump($result); |
Then, you will be see the results sent from your items method.
Note: For more information, the links below are pretty useful:
- http://devdocs.magento.com/guides/m1x/api/soap/create_your_own_api.html
- https://code.tutsplus.com/tutorials/create-a-custom-api-in-magento-part-one–cms-23785