create a new Web Service V2 API with magento

What is Magento Web Service:

Magento has a very rich set of Web Service APIs available with it. But still it’s  great that it wouldn’t limit us with that only. We do have other options available to extend this treasure with more diamonds as well.

 

Pre-Requisite:

1)      I assume that you must be aware about Web Service and their functionality but still its my pleasure to write two lines

“Web Service is basically an interface through which you can connect through other application that is not under your control. But still you can consume their services to either retrieve or input your values to that third party application based on available permission to use that web service”

2) I understand you already have any existing magento extension under which you are going to add new web services so that some third party can use these web services for their own requirement. If you need any detail about how to create new magento extension

If you are beginner then you must have to go through our earlier blogs regarding
i) How to Setup a Web Service on Magento

ii) How to call Web Service using PHP

iii) How to call Web Service using Javascript

In this series today I am going to explain how to create new new Magento API (v2):

Before we go further into actual implementation of Web Service. I am going to list the files in which are required to be created on your existing module :

i)                 –Module-Path/etc/api.xml

ii)                –Module-Path/model/api.php

iii)               –Module-Path/etc/wsdl.xml

In this tutorial we will create a web service to “delete shopping cart rule by name” . Below are the codes written into each file

Code written into –Module-Path/etc/api.xml (See Code Description below the code)


<?xml version="1.0"?>

<config>

<api>

<!-- START RESOURCES -->

<resources>

<!-- START SHOPPING CART RESOURCES -->

<PromoCode translate="title" module="Namespace_PromoCode">

<title> Promo Code Api</title>

<model>PromoCode/api</model>

<methods>

<deletecartrulebyname translate="title" module="Rads_CoreApi">

<title>Delete Cart Rule by name</title>

<method>Deletecartrulebyname</method>

<acl>sales/order/invoice/info</acl>

</deletecartrulebyname>

</methods>

</PromoCode>

<!-- END SHOPPING CART RESOURCES -->

</resources>

<!-- END RESOURCES -->

<v2>

<!-- START V2 FUNCTIONS PREFIX -->

<!-- START V2 SHOPPING CART PREFIX -->

<resources_function_prefix>

<PromoCode>PromoCode</PromoCode>

</resources_function_prefix>

<!-- END OF V2 FUNCTIONS PREFIX -->

<!-- END OF V2 SHOPPING CART PREFIX -->

</v2>

</api>

</config>

Code Analysis : This file (api.xml) is the primary files that tells the magento extension that this module has some external API available. All the codes under this files are always comes into  <config><api> tag only.

Line 7-9: its creates resources for this API. These resources are binded together under Module Name,Title and Model tag .

Line 10: From this section you can see all functions (API functions) are written into <methods> tag only.

Line 11-15 : This section contain actual function name . You can see each function inherits core modules “Rads_CoreApi”. And each API function has 3 parts

  • Title : Title of function
  • Method : It contain actual method name that will be available under your PHP file
  • ACL : it contain access control for this function. This is for prevent unauthorized access to our custom API.

Line 21-29 : Under this code we define the prefix of PHP function. So all the methods defined above on api.xml should be defined on v2.php file . But PHP function name should have function name like

Public function PrefixMethod().

Code written into –Module-Path/etc/wsdl.xml (See Code Description below the code)


<?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}}">

<message name="PromoCodeDeletecartrulebynameRequest">

<part name="sessionId" type="xsd:string"/>

<part name="sku" type="xsd:string"/>

<part name="identifierType" type="xsd:string"/>

</message>

<message name="PromoCodeDeletecartrulebynameResponse">

<part name="result" type="xsd:string" />

</message>

&nbsp;

<portType name="{{var wsdl.handler}}PortType">

<operation name="PromoCodeDeletecartrulebyname">

<documentation>Delete Shopping Cart Rule by name. </documentation>

<input message="typens:PromoCodeDeletecartrulebynameRequest" />

<output message="typens:PromoCodeDeletecartrulebynameResponse" />

</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="PromoCodeDeletecartrulebyname">

<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>


Code Analysis: This file contain the code for wsdl (Web Service Descriptive Language) file. The code written on this file are directly accessible from Magento’s wsdl file path. Actual use of  this file to let developer know that what are the available methods on the API and what are the input as well as output of each and every web service API function.

Line 6-10 : Under this block you can see all possible input parameter for the API function with there name as well as type of input.

Line 11-13: Under this block you can see all possible output parameter for the API function with there name as well as type of input.

Line 17 -21 : As we have seen that  for each API function we define possible input and output parameters. These input and output are merged together under an operation.

Source Code written into –Module-Path/model/api.php (See Code Description below the code)


class NameSpace_PromoCode_Model_Api extends Mage_CatalogRule_Model_Rule

{

}

Code Analysis : If we need any common function that needs to interact with db then we can define that function into API.php class. For now we haven’t used any other function.

Source Code written into –Module-Path/model/api/v2.php (See Code Description below the code)


class Namespace_PromoCode_Model_Api_V2 extends Namespace_PromoCode_Model_Api

{

&nbsp;

public function Deletecartrulebyname($name)

{

Mage::Log('in PromoCode');

$model = Mage::getModel('salesrule/rule')

->getCollection()

->addFieldToFilter('name', array('eq'=>$name))

->getFirstItem();

if($model->getId())

{

if($model->delete())

{

$return = "Promo Code Deleted successfully";

}

else

{

$return = "Unable to delete Promo Code";

}

}

else

$return = "No Promo Code available with name:".$name;

return $return;

}

}

Code Analysis: Under this file v2.php we write actual function where we perform actual implementation and return the output as defined on wsdl file.

Note: Since magento is case sensitive so keep your eyes on case sensitivity defining under xml files. It should be Camelcased.

Related posts:

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)