HTML Code:
<config>
<api>
....
<acl>
<resources>
<customer translate="title" module="customer">
<title>Customers</title>
<list translate="title" module="customer">
<title>View All</title>
</list>
<create translate="title" module="customer">
<title>Create</title>
</create>
<info translate="title" module="customer">
<title>Get Info</title>
</info>
<update translate="title" module="customer">
<title>Update</title>
</update>
<delete translate="title" module="customer">
<title>Delete</title>
</delete>
</customer>
</resources>
</acl>
</api>
</config>
Then, map ACL resources to API resource methods by adding an element to each part of the resource that needs restricting:
HTML Code:
<config>
<api>
<resources>
<customer translate="title" module="customer">
<title>Customer Resource</title>
<acl>customer</acl>
<methods>
<list translate="title" module="customer">
<title>Retrive customers</title>
<acl>customer/list</acl>
</list>
<create translate="title" module="customer">
<title>Create customer</title>
<acl>customer/create</acl>
</create>
<info translate="title" module="customer">
<title>Retrieve customer data</title>
<acl>customer/info</acl>
</info>
<update translate="title" module="customer">
<title>Update customer data</title>
<acl>customer/update</acl>
</update>
<delete>
<title>Delete customer</title>
<acl>customer/delete</acl>
</delete>
</methods>
....
</customer>
</resources>
....
</api>
</config>
5. Creating PHP Code
Next, write some PHP code to access the resources. Start by creating a class called Mage_Customer_Model_Api that extends Mage_Api_Model_Resource_Abstract. Save it into a file called api.php.
PHP Code:
class Mage_Customer_Model_Api extends Mage_Api_Model_Resource_Abstract
{
public function create($customerData)
{
}
public function info($customerId)
{
}
public function items($filters)
{
}
public function update($customerId, $customerData)
{
}
public function delete($customerId)
{
}
}
Note that you cannot create method “list” because it’s a PHP keyword, so instead the method is named items. In order to make this work, add a element into the
element in api.xml, as shown below.
HTML Code:
<config>
<api>
<resources>
<customer translate="title" module="customer">
<model>customer/api</model> <!-- our model -->
<title>Customer Resource</title>
<acl>customer</acl>
<methods>
<list translate="title" module="customer">
<title>Retrive customers</title>
<method>items</method> <!-- we have another method name inside our resource -->
<acl>customer/list</acl>
</list>
....
</methods>
....
</resources>
....
</api>
</config>
Now add some simple functionality to the Mage_Customer_Model_Api methods you created.
Create a customer:
PHP Code:
public function create($customerData)
{
try {
$customer = Mage::getModel('customer/customer')
->setData($customerData)
->save();
} catch (Mage_Core_Exception $e) {
$this->_fault('data_invalid', $e->getMessage());
// We cannot know all the possible exceptions,
// so let's try to catch the ones that extend Mage_Core_Exception
} catch (Exception $e) {
$this->_fault('data_invalid', $e->getMessage());
}
return $customer->getId();
}
Retrieve customer info:
PHP Code:
public function info($customerId)
{
$customer = Mage::getModel('customer/customer')->load($customerId);
if (!$customer->getId()) {
$this->_fault('not_exists');
// If customer not found.
}
return $customer->toArray();
// We can use only simple PHP data types in webservices.
}
Retrieve list of customers using filtering:
PHP Code:
public function items($filters)
{
$collection = Mage::getModel('customer/customer')->getCollection()
->addAttributeToSelect(' ');
if (is_array($filters)) {
try {
foreach ($filters as $field => $value) {
$collection->addFieldToFilter($field, $value);
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
// If we are adding filter on non-existent attribute
}
}
$result = array();
foreach ($collection as $customer) {
$result[] = $customer->toArray();
}
return $result;
}
Update a customer:
PHP Code:
public function update($customerId, $customerData)
{
$customer = Mage::getModel('customer/customer')->load($customerId);
if (!$customer->getId()) {
$this->_fault('not_exists');
// No customer found
}
$customer->addData($customerData)->save();
return true;
}
Delete a customer:
PHP Code:
public function delete($customerId)
{
$customer = Mage::getModel('customer/customer')->load($customerId);
if (!$customer->getId()) {
$this->_fault('not_exists');
// No customer found
}
try {
$customer->delete();
} catch (Mage_Core_Exception $e) {
$this->_fault('not_deleted', $e->getMessage());
// Some errors while deleting.
}
return true;
}
Creating a Custom Adapter
In order to create custom webservice adapter, implement the Mage_Api_Model_Server_Adapter_Interface, which is shown below.
PHP Code:
interface Mage_Api_Model_Server_Adapter_Interface
{
/
Set handler class name for webservice
@param string $handler
@return Mage_Api_Model_Server_Adapter_Interface
/
function setHandler($handler);
/
Retrive handler class name for webservice
@return string [basc]
/
function getHandler();
/
Set webservice api controller
@param Mage_Api_Controller_Action $controller
@return Mage_Api_Model_Server_Adapter_Interface
/
function setController(Mage_Api_Controller_Action $controller);
/
Retrive webservice api controller
@return Mage_Api_Controller_Action
/
function getController();
/
Run webservice
@return Mage_Api_Model_Server_Adapter_Interface
/
function run();
/
Dispatch webservice fault
@param int $code
@param string $message
/
function fault($code, $message);
} // Class Mage_Api_Model_Server_Adapter_Interface End
Here is an example implementation for XML-RPC:
PHP Code:
class Mage_Api_Model_Server_Adapter_Customxmlrpc
extends Varien_Object
implements Mage_Api_Model_Server_Adapter_Interface
{
/
XmlRpc Server
@var Zend_XmlRpc_Server
/
protected $_xmlRpc = null;
/
Set handler class name for webservice
@param string $handler
@return Mage_Api_Model_Server_Adapter_Xmlrpc
/
public function setHandler($handler)
{
$this->setData('handler', $handler);
return $this;
}
/
Retrive handler class name for webservice
@return string
/
public function getHandler()
{
return $this->getData('handler');
}
/
Set webservice api controller
@param Mage_Api_Controller_Action $controller
@return Mage_Api_Model_Server_Adapter_Xmlrpc
/
public function setController(Mage_Api_Controller_Action $controller)
{
$this->setData('controller', $controller);
return $this;
}
/
Retrive webservice api controller
@return Mage_Api_Controller_Action
/
public function getController()
{
return $this->getData('controller');
}
/
Run webservice
@param Mage_Api_Controller_Action $controller
@return Mage_Api_Model_Server_Adapter_Xmlrpc
/
public function run()
{
$this->_xmlRpc = new Zend_XmlRpc_Server();
$this->_xmlRpc->setClass($this->getHandler());
$this->getController()->getResponse()
->setHeader('Content-Type', 'text/xml')
->setBody($this->_xmlRpc->handle());
return $this;
}
/
Dispatch webservice fault
@param int $code
@param string $message
/
public function fault($code, $message)
{
throw new Zend_XmlRpc_Server_Exception($message, $code);
}
} // Class Mage_Api_Model_Server_Adapter_Customxmlrpc End
Notes: The setHandler, getHandler, setController and getController methods have a simple implementation that uses the Varien_Object getData and setData methods.
The run and fault methods are a native implementation for an XML-RPC webservice. The run method defines webservice logic in this adapter for creating an XML-RPC server to handle XML-RPC requests.
PHP Code:
public function run()
{
$this->_xmlRpc = new Zend_XmlRpc_Server();
$this->_xmlRpc->setClass($this->getHandler());
$this->getController()->getResponse()
->setHeader('Content-Type', 'text/xml')
->setBody($this->_xmlRpc->handle());
return $this;
}
The “fault” method allows you to send fault exceptions for XML-RPC service when handling requests.
PHP Code:
public function fault($code, $message)
{
throw new Zend_XmlRpc_Server_Exception($message, $code);
}
Common Error Messages
The following are common error messages that you might receive when creating your own custom API.
Invalid API path
This error occurs when the methods listed in the api.xml file do not correspond exactly with those used in your PHP file.
For example, in your api.xml file, you might have this:
HTML Code:
<config>
<api>
<resources>
<checkout_cart translate="title" module="checkout">
<model>checkout/cart_api</model>
<title>Cart API</title>
<methods>
<list translate="title" module="checkout">
<title>Retrieve cart data</title>
<method>info</method>
</list>
</methods>
</checkout_cart>
</resources>
...
</api>
</config>
You should have a corresponding info method in your PHP file.
PHP Code:
class Mage_Checkout_Model_Cart_Api extends Mage_Cart_Model_Api_Resource
{
public function info()
{
...
}
}
If you are missing this method, the error “Invalid api path” will be returned.
Source: Magento Official
View more threads in the same category:
Bookmarks