Home » Magento® blog » Lesson 31: API in Magento (part 2)
Lesson 31: API in Magento (part 2)

Lesson 31: API in Magento (part 2)

Hi buddies,

We finished part 1 of lesson 31 last week. The second part continues to guide you how to declare and create a new API. Follow these steps to understand deeper.

Lesson 31: API in Magento (part 1)

3. How to declare and create a new 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.

3.1. Creating an XML file that will defend the API resource

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

<config>

<api>
<resources>
</resources>
<acl>
<all>
</all>
</acl>
</api>
</config>

3.2. Adding a Resource Named Lesson 31

Add an element named customer in the <resources> element. Add a <methods> element, with elements for list lesson31 resource.

Note that: list will return all customers

<config>

<api>
<resources>
<lesson31 translate="title" module="lesson31">
<title>Lesson31 Resource</title>
<methods>
<list translate="title" module="lesson31">
<title>Retrive Lesson 31</title>
</list>
</methods>
</lesson31>
</resources>
...
</api>
</config>

3.3. Adding Faults

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

<config>

<api>
<resources>
<lesson31 translate="title" module="lesson31">
<title>Lesson31 Resource</title>
<methods>
<list translate="title" module="lesson31">
<title>Retrive Lesson 31</title>
</list>
</methods>
<faults module="lesson31">
<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 lesson 31 data. Details in error message.</message>
</data_invalid>
</faults>
</lesson31>
</resources>
...
</api>
</config>

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

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

<config>

<api>
...
<acl>
<resources>
<lesson31 translate="title" module="lesson31">
<title>Lesson 31</title>
<list translate="title" module="lesson31">
<title>List all lesson 31</title>
</list>
</lesson31>
</resources>
</acl>
</api>
</config>

3.5. Creating PHP Code

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

<?php

/**
* Magestore
*
* Online Magento Course
*
*/

/**
* Lesson31 Model
*
* @category Magestore
* @package Magestore_Lesson31
* @author Magestore Developer
*/
class Magestore_Lesson31_Model_Api extends Mage_Api_Model_Resource_Abstract
{
public function items($filters)
{
}
}

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.

<config>

<api>
<resources>
<lesson31 translate="title" module="lesson31">
<title>Lesson31 Resource</title>
<methods>
<list translate="title" module="lesson31">
<title>Retrive Lesson 31</title>
<method>items</method> <!-- we have another method name inside our resource -->
<acl>lesson31/list</acl>
</list>
</methods>
<faults module="lesson31">
<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 lesson 31 data. Details in error message.</message>
</data_invalid>
</faults>
</lesson31>
</resources>
...
</api>
</config>

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

Retrieve list of lesson 31 using filtering:

class Magestore_Lesson31_Model_Api extends Mage_Api_Model_Resource_Abstract {

public function items($filters) {
$collection = Mage::getModel('lesson31/lesson31')->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 $lesson31) {
$result[] = $lesson31->toArray();
}

return $result;
}

}

After this lesson, we should know:

  • How API works in Magento
  • How to use available API functions
  • How to create a new API

You also may concern:


Ready to foster your knowledge with MAGENTO 2 BASIC GUIDE

The new Order Success combo with Magento 2 Inventory Management, Magento 2 Order Fulfillment, Magento 2 Dropship, Magento 2 Barcode Management and Magento 2 Purchase Management

Is your site still a mess and Magento extensions are not enough to save your site? Save your site with Magento Web Development Services now

magento services


The following two tabs change content below.

Blanka

This is our talented developer who has been developing many outstanding extensions. If you meet Blanka in person, you will never forget his special laugh, very cheerful and relaxing.

Leave a Reply

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

*