Nice:X harambe options price not magento updating custom blest

Wednesday, September 13, 2017 by Axim

Edit this page on GitHub

Creating a Custom API or Extending the Core API

The Core API allows you to manage a set of common resources used in Magento. However, you may choose to have your own set of resources to manage, or you may wish to extend the Core API to handle additional resources.

This tutorial leads you through the process of creating a custom API for a customer module that handles basic customer information.

Note: This tutorial applies to v1 of the API.

To learn more about the Core API, to read Magento Core API calls.

For general information about the Magento API, go to the Introduction.

1. Creating an XML File that Will Define the API Resource

Create a file named api.xml in the /etc folder in the customer module. Start with the empty structure, as follows:

2. Adding a Resource Named Customer

Add an element named customer in the <resources> element. Add a <methods> element, with elements for list, create, info, update and remove methods for customer resource.

Note that:

  • list will return all customers
  • create will create a new customer
  • info will return data on a specified customer
  • update will update data on a specified customer
  • remove will delete data on a specified customer

3. Adding Faults

The resource can return some faults, so add a <faults> element in the customer element, and list the various faults.

4. Describing the Access Control List (ACL) for the Resource

In order to prevent unauthorized access to our custom API, you must first list the resources that are restricted within the <acl> element.

Then, map ACL resources to API resource methods by adding an <acl> element to each part of the resource that needs restricting:

5. Creating PHP Code

Next, write some PHP code to access the resources. Start by creating a class called Mage_Customer_Model_Customer_Api that extends Mage_Api_Model_Resource_Abstract. Save it into a file called api.php.

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 <method> element into the <list> element in api.xml, as shown below.

Now add some simple functionality to the Mage_Customer_Model_Api methods you created.

Create a customer:

Retrieve customer info:

Retrieve list of customers using filtering:

Update a customer:

Delete a customer:

Creating a Custom Adapter

In order to create custom webservice adapter, implement the Mage_Api_Model_Server_Adapter_Interface, which is shown below.

Here is an example implementation for XML-RPC:

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.

The "fault" method allows you to send fault exceptions for XML-RPC service when handling requests.

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:

You should have a corresponding info method in your PHP file.

If you are missing this method, the error "Invalid api path" will be returned.

<config> <api> <resources> </resources> <acl> <resources> <all> </all> </resources> </acl> </api> </config>
<config> <api> .... <resources> <customer translate="title" module="customer"> <title>Customer Resource</title> <methods> <list translate="title" module="customer"> <title>Retrive customers</title> </list> <create translate="title" module="customer"> <title>Create customer</title> </create> <info translate="title" module="customer"> <title>Retrieve customer data</title> </info> <update translate="title" module="customer"> <title>Update customer data</title> </update> <delete> <title>Delete customer</title> </delete> </methods> <faults module="customer"> </faults> </customer> </resources> .... </api> </config>
<config> <api> .... <resources> <customer translate="title" module="customer"> .... <faults module="customer"> <!-- module="customer" specifies the module which will be used for translation. --> <data_invalid> <!-- if we get invalid input data for customers --> <code>100</code > <!-- we cannot know all the errors that can appear, their details can be found in error message for call --> <message>Invalid customer data. Details in error message.</message> </data_invalid> <filters_invalid> <code>101</code > <message>Invalid filters specified. Details in error message.</message> </filters_invalid> <not_exists> <code>102</code > <message>Customer doesn't exist.</message> </not_exists> <not_deleted> <code>103</code > <message>Customer was not deleted. Details in error message.</message> </not_deleted> </faults> </customer> </resources> .... </api> </config>
<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>
<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>
class Mage_Customer_Model_Customer_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) { } }
<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>
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
public function fault($code, $message)     {         throw new Zend_XmlRpc_Server_Exception($message, $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>
class Mage_Checkout_Model_Cart_Api extends Mage_Cart_Model_Api_Resource { public function info() { ... } }
http://devdocs.magento.com/guides/m1x/api/soap/create_your_own_api.html