Friday, May 8, 2015

How to create your custom API in Magento

Creating Custom API is very easy in Magento.
Just create api.xml file in your etc folder

<?xml version="1.0"?>
<config>
    <api>
        <resources>          
            <!-- START GUSTOMER GROUP RESOURCES -->
          <mycustom_productupload translate="title" module="package_module">
                <model>package_module/productupload_api</model>
                <title> My API</title>
                <acl>custom_data</acl>
                <methods>
                    <list translate="title" module="package_module">
                        <title>custom product upload</title>
                        <method>bulkuploadproduct</method>
                    </list>
                </methods>
            </mycustom_productupload>
            <!-- END CUSTOMER GROUP RESOURCES -->
        </resources>
         
       
       
        <acl>
            <resources>
                <custom_data translate="title" module="package_module">
                    <title>Custom data</title>
                    <sort_order>3</sort_order>
                </custom_data>
            </resources>
        </acl>
    </api>
</config>

Now create a folder under model Productupload because you have define it under the model tag
and create a file api.php under Productupload. (Productupload /api.php)

api.php

class Package_Module_Model_Productupload_Api extends Mage_Customer_Model_Group_Api
{
   
    public function bulkuploadproduct($productListArr){
       your code....
    }

}


this bulkuploadproduct is the function which will automatically call when when you call the api.
Please find the following example to call this API.

$client = new SoapClient(SOAP_WSDL);
$session = $client->login(SOAP_USER, SOAP_PASS);
$client->call($session, 'mycustom_productupload.list',array($productList));

Create a magento admin module & put link to magento menu with user role (ACL resources)

If you know how to create Magento module then create it first. After then you need to create a Magento controller for admin area.

class Package_Module_MycustomController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
         your code here.......
    }
}

Now if you want to create top menu in your magento admin, which can be enable or disable from admin role management area (ACL resources).

config.xml
<adminhtml>
   
        <menu>
            <package_module translate="title" module="package_module">
                <title>Manage Portal</title>
                <sort_order>9999</sort_order>
                <action>mycustom/userauth/index</action>               
            </package_module>
        </menu>
        <acl>
        <resources>
            <admin>
                <children>
                    <package_module translate="title" module="package_module">
                        <title>Manage Portal</title>
                        <sort_order>9999</sort_order>
                        <action>mycustom/userauth/index</action>   
                    </package_module>
                </children>
            </admin>
        </resources>
    </acl>
   
    </adminhtml>