How To Create Custom Function Using SOAP API

SOAP stands for Simple Object Access Protocol. It is based on XML mostly via HTTP (POST). Its basic structure is basically Envelope with Header and Body inside.

We need to create api user and role. This can be created from Admin -> System Configuration -> Web Services -> SOAP-XML/RPC User/Role. After doing this you should have “username” and “api_key” with you.

Read: How to Build REST API Using PHP

A Simple Example to List all the products in your magento store using SOAP API Function is as below:

SOAP V1 – Example

$client = new SoapClient('http://magentohost/api/soap/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$result = $client->call($session, 'catalog_product.list');

SOAP V2 – Example

$proxy = new SoapClient('http://magentohost/api/v2_soap/?wsdl');  // Url will also Varies for V1 and V2
$sessionId = $proxy->login('apiUser', 'apiKey');
$result = $proxy->catalogProductList($sessionId);

Custom Module Creation in SOAP API

Scenario: Create a custom function using SOAP API to update order amount in database (function should call from ERP tool).

Function Name : soapmoduleProjectPayorder
Parameters: Order Id , Amount , Comments
Output : Boolean

Also Read: How to Create a Chart using Google API?

Step 1:

Create a Module with the name Mage_Soapmodule

Step 2:

Create an api.xml file in etc/ folder of the module. api.xml is the main configuration file for your SOAP API.


<?xml version="1.0"?>
<soapmodule_project translate="title" module="soapmodule">

<acl>soapmodule/project</acl><!-- acl resource -->
<payorder translate="title" module="soapmodule">


 // payorder is the function name where the bussiness logic defined to update the order details  in database

<faults module="soapmodule">
<message>Requested Order Details does not exist.</message>
<message>Provided data is invalid.</message>
<message>Error while saving project. Details in error message.</message>
<message>Error while removing project. Details in error message.</message>
<acl><!-- acl definition -->
<soapmodule translate="title" module="soapmodule">

<project translate="title" module="soapmodule">

<list translate="title" module="soapmodule">


Step 3 :

Create the config.xml file in etc folder by specifying the version ,frontname etc..



Step 4 :

Need to create the Wsdl file. When we consider the Soap API we need to declare the parameters in xml format


<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:xsd="" xmlns:soap=""
xmlns:soapenc="" xmlns:wsdl="" xmlns=""
name="{{var}}" targetNamespace="urn:{{var}}">
<schema xmlns="" targetNamespace="urn:Magento">
<import namespace="" schemaLocation="" />
<complexType name="soapmoduleProjectPayorderEntity"><all>
<element name="orderId" type="xsd:string" minOccurs="1" />
<element name="amount" type="xsd:string" minOccurs="0" />
<element name="comment" type="xsd:string" minOccurs="0" />
<!--define messages -->
<message name="soapmoduleProjectPayorderRequest">
<part name="sessionId" type="xsd:string" />
<part name="orderId" type="xsd:string" />
<part name="amount" type="xsd:string" />
<part name="comment" type="xsd:string" />
<message name="soapmoduleProjectPayorderResponse">
 	<part name="result" type="xsd:boolean" />
<!--define messages -->
<!-- define portTypes -->
<portType name="{{var wsdl.handler}}PortType">
<operation name="soapmoduleProjectPayorder">
<documentation>Seller Payment</documentation>
<input message="typens:soapmoduleProjectPayorderRequest" />
<output message="typens:soapmoduleProjectPayorderResponse" />
<!--[-] define portTypes -->
<!--[+] define binding -->
<binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
<soap:binding style="rpc" transport="" />
		 <operation name="soapmoduleProjectPayorder">
<soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
<soap:body namespace="urn:{{var}}" use="encoded" encodingStyle="" />
<soap:body namespace="urn:{{var}}" use="encoded" encodingStyle="" />
<!--[-] define portTypes -->
<service name="{{var}}Service">
<port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
<soap:address location="{{var wsdl.url}}" />

Step 5 :

In this Step we need to create the Wsi.xml file in etc/wsi.xml. Here we need to declare the necessary parameters. As mentioned above necessary params to call the function are orderId, amount and comment.


<?xml version="1.0" encoding="UTF-8"?>
xmlns:wsdl="" name="{{var}}" targetNamespace="urn:{{var}}">
<xsd:schema xmlns:xsd="" targetNamespace="urn:{{var}}">
 <xsd:complexType name="soapmoduleProjectPayorderEntity">
<xsd:element name="orderId" type="xsd:string" />
<xsd:element name="amount" type="xsd:string" />
<xsd:element name="comment" type="xsd:string" />
<xsd:element name="soapmoduleProjectPayorderRequestParam">
 //Declaring the necessary parameters
<xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1" name="orderId" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1" name="amount" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1" name="comment" type="xsd:string" />
<xsd:element name="soapmoduleProjectPayorderResponseParam">
<xsd:element minOccurs="1" maxOccurs="1" name="result" type="xsd:boolean" />
 <wsdl:message name="soapmoduleProjectPayorderRequest">
<wsdl:part name="parameters" element="typens:soapmoduleProjectPayorderRequestParam" />
<wsdl:message name="soapmoduleProjectPayorderResponse">
<wsdl:part name="parameters" element="typens:soapmoduleProjectPayorderResponseParam"/>
<wsdl:portType name="{{var wsdl.handler}}PortType">
<wsdl:operation name="soapmoduleProjectPayorder">
<wsdl:documentation>Seller Payment</wsdl:documentation>
<wsdl:input message="typens:soapmoduleProjectPayorderRequest" />
<wsdl:output message="typens:soapmoduleProjectPayorderResponse" />
<wsdl:binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
<soap:binding style="document" transport="" />
<wsdl:operation name="soapmoduleProjectPayorder">
<soap:operation soapAction="" />
<soap:body use="literal" />
<soap:body use="literal" />
<wsdl:service name="{{var}}Service">
<wsdl:port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
<soap:address location="{{var wsdl.url}}" />

Step 6 :

Create Model for handling the SOAP V1 requests

Create the api model file. In the api.xml file we have mentioned the model to be “soapmodule/project_api” so we need to create the file


public function payorder($orderId,$amount,$comment){
* Storing Transaction details
$transactionDetails = 
array(‘order_id’ => $orderId, ‘amount’=> $amount,’comment’=> $comment);
$saveTransactionDetails = Mage::getModel (testmodel/testmodel' )->setData($transactionDetails);
catch (Mage_Core_Exception $e) {
$this->_fault('save_error', $e->getMessage());
return true;

Step 7 :

create a a new file as v2.php in Api folder


class Mage_Soapmodule_Model_Project_Api_V2 extends Mage_Soapmodule_Model_Project_Api{

Final Step :

In your Magento root folder create soap_api.php file and call the custom Soap API function

$api_url = 'http://localhost/demostore/api/v2_soap/?wsdl';
$api_un	= ‘demo’;
$api_pw	= 'demo123';

$proxy 	= new SoapClient($api_url);
$sessionId = $proxy->login($api_un,$api_pw);

$result = $proxy->soapmoduleProjectPayorder($sessionId,'160411747',50,'by cash');
echo '<pre>';

Creating a custom function using SOAP API will give developers much needed flexibility. This article was created by developers to help fellow developers learn how to create a custom function using SOAP API.

Hope we have made things clear. If there is anything that you wish to know more, feel free to drop in a comment or inbox us.

Realated Posts

Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">