I have to create a REST API that can be used in android phone to list all the products including product image using category id ?
3 Answers
There are many tutorials which guides how to create the rest api.
you can have a look a these.
http://inchoo.net/magento/api-magento/magento-2-custom-api/
https://webkul.com/blog/magento2-custom-rest-api/
Create Custom Rest Api in Magento2 and solution for "InvalidArgumentException" error
There is official magento document for this.
http://devdocs.magento.com/guides/v2.0/get-started/bk-get-started-api.html
For your requirement API's are already available, you can refer below.
You can use these samples using:
- REST Token-based
- REST OAuth-based
- SOAP
https://gist.github.com/rafaelstz/ecab668b80fece4d9acdb9c5358b3173
Create REST API using standard ways in Magento2.
Here is the directories structure of a create the Magento2 module:
<Vendor>/<ModuleName>
API/
---Data/
-------CustomerInterface.php
---CustomerRepositoryInterface.php
etc/
di.xml
webapi.xml
module.xml
Model/
-----CustomerRepository.php
-----Customer.php
registration.php
Create a Customer interface.
<?php
namespace Practice\Customer\Api\Data;
interface CustomerInterface
{
const ID = 'id';
const NAME = 'name';
/**
* @return int
*/
public function getId();
/**
* @param int $id
* @return \Practice\Customer\Api\Data\CustomerInterface
*/
public function setId($id);
/**
* @return string
*/
public function getName();
/**
* @param string $name
* @return \Practice\Customer\Api\Data\CustomerInterface
*/
public function setName($name);
}
Create a Customer repository interface.
<?php
namespace Practice\Customer\Api;
/**
* Collection Repository Interface
* @api
*/
interface CustomerRepositoryInterface
{
/**
* Update the Collection Items
*
* @param \Practice\Customer\Api\Data\CustomerInterface[] $collection
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function collectionUpdate(array $collection);
/**
* Delete the collection Items
*
* @param \Practice\Customer\Api\Data\CustomerInterface[] $collection
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function collectionDelete(array $collection);
}
Create a Customer model.
<?php
namespace Practice\Customer\Model;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Practice\Customer\Model\ResourceModel\Collection as LoggingCollection;
use Practice\Customer\Api\Data\CustomerInterface;
/**
* Class Customer
*/
class Customer extends AbstractModel implements CustomerInterface
{
/**
* OrderHistoryList constructor.
* @param Context $context
* @param Registry $registry
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
Context $context,
Registry $registry,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct(
$context,
$registry,
$resource,
$resourceCollection,
$data
);
}
/**
* Initialize resource
* @return void
*/
// phpcs:ignore PSR2.Methods.MethodDeclaration
protected function _construct()
{
$this->_init(LoggingCollection::class);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->getData(self::ID);
}
/**
* @inheritdoc
*/
public function setId($id)
{
$this->setData(self::ID, $id);
}
/**
* @inheritdoc
*/
public function setName($name)
{
$this->setData(self::NAME, $name);
}
/**
* @inheritdoc
*/
public function getName()
{
return $this->getData(self::NAME);
}
}
Create a Customer repository model.
<?php
namespace Practice\Customer\Model;
use Magento\Framework\Webapi\Exception;
/**
* Class CustomerRepository
* @package Practice\Customer\Model
*/
class CustomerRepository implements CustomerRepositoryInterface
{
/**
* Update Customer.
*
* @param array $collection
* @return array
*/
public function collectionUpdate(array $collection)
{
// Write here business logic
}
/**
* Delete Customer Collection.
*
* @param array $collection
* @return array
*/
public function collectionDelete(array $collection)
{
// Write here business logic
}
}
Bind Customer interface with customer model and customer repository interface and customer repository model.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Practice\Customer\Api\Data\CustomerInterface" type="Practice\Customer\Model\Customer"/>
<preference for="Practice\Customer\Api\CustomerRepositoryInterface" type="Practice\Customer\Model\CustomerRepository"/>
</config>
Web API manage
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/customer/update" method="PUT">
<service class="Practice\Customer\Api\CustomerRepositoryInterface" method="collectionUpdate"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
<route url="/V1/Customer/delete" method="POST">
<service class="Practice\Customer\Api\CustomerRepositoryInterface" method="collectionDelete"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>