How to create custom helloworld api in magento
As we all know magento backend provides an interface for different operations. But sometimes we will need to access and transmit data from and to third-party systems.So for that magento provides API functionality for most of the core features even we can create custom API also for our own purposes. Magento custom module development is a core part of any Magento development or Magento project, because at any stage you may want to integrate your own functionality/module in your existing Magento project.
Magento CoreAPI provides a set of common resources for managing customer, catalog etc. Sometimes we Need to Provide our Own API from our website to third parties. In this tutorial I will see how to create our own custom helloworld api in magento. For Simplicity Access Control restrictions are left out in this example.
Step 1: Let us start our module with the config.xml and api.xml file in our module. config.xml defines the model class and helper class specification. api.xml defines the api method, model class,resourcename etc.
Filepath: app/code/local/Prashant/Customapi/etc/config.xml
app/code/local/Prashant/Customapi/etc/api.xml
Add below code in “config.xml” file:-
<?xml version="1.0"?> <config> <modules> <prashant_customapi> <version>0.1.0</version> </prashant_customapi> </modules> <global> <models> <customapi> <class>Prashant_Customapi_Model</class> </customapi> </models> <helpers> <customapi> <class>Prashant_Customapi_Helper</class> </customapi> </helpers> </global> </config>
Add below code in “api.xml” file:-
<?xml version="1.0"?> <config> <api> <resources> <customapi translate="title" module="customapi"> <model>customapi/helloworld_api</model> <title>Custom HelloWorld API </title> <methods> <hello translate="title" module="customapi"> <title>Prashant HelloWorld API</title> <method>hello</method> </hello> </methods> <faults module="customapi"> <data_invalid> <code>100</code> <message>Invalid data</message> </data_invalid> </faults> </customapi> </resources> </api> </config>
Step 2: Now I start creating actual api model class Api.php in the directory. My Api.php file is placed in the below directory app/code/local/Prashant/Customapi/Model/Helloworld/Api.php. Api.php contains a single method called hello which is specified in the api.xml file previously, the method accepts an argument and displays it along with its default message. customapi is the resourcename.
<?php class Prashant_Customapi_Model_Helloworld_Api extends Mage_Api_Model_Resource_Abstract { public function hello($msg) { return "My Custom HelloWorld API In Magento. Here is Your Message ". $msg ; } }
Step 3: Our Final Module Activation file looks as shown below, which is placed in app/etc/module/Prashant_Customapi.xml
<?xml version="1.0"?> <config> <modules> <prashant_customapi> <active>true</active> <codepool>local</codepool> </prashant_customapi> </modules> </config>
Step 4: Create a webservice user and role in magento admin panel.
Step 5: Now finally I am going to test my helloworld api, Create a file in magento root folder called testapi.php and run as http://yourdomainname.com/testapi.php
<?php // custom api soap client example $mageFilename = 'app/Mage.php'; require_once $mageFilename; umask(0); Mage::app(); $myhellomsg = "<b>Helloworld Prashant</b>"; try { $soap = new SoapClient('http://yourdomainname.com/api/?wsdl'); $sessionId = $soap->login('apiUser', 'apiKey'); //echo "Login ID : $sessionId"; $result = $soap->call($sessionId, 'customapi.hello',array($myhellomsg)); echo $result; } catch(Exception $e) { echo $e->getMessage(); }
I want to extend the following class and function to get products or categories do you know how
class Mydons_Customapi_Model_Helloworld_Api extends Mage_Api_Model_Resource_Abstract
{
public function hello($msg) {
return “My Custom HelloWorld API In Magento. Here is Your Message “. $msg ;
}
}
I am getting this error, “Invalid api path.”. Can you please tell me the reason.