3

I am new to Magento and want to know how to write Rest web services with PHP. Please suggest me with any material or example with full explanation because i gone through many, but couldn't find a good one which is working. I can also say that i don't know how to execute that. please give me a full explanation with example. Thanks in advance :)

2 Answers 2

0

There is a good solution using this library PHP.

Magento Client Library For PHP

Usage

XMLRPC

The following example returns a list of products with SKUs that start with "123":

use Magento\Client\Xmlrpc\MagentoXmlrpcClient;

$client = MagentoXmlrpcClient::factory(array(
    'base_url' => 'http://magentohost',
    'api_user' => 'api.user',
    'api_key'  => 'some.private.key',
));

$filters = array(
    'sku' => array('like' => '123%'),
);

$result = $client->call('catalog_product.list', array($filters));

REST

The following example returns a list of products:

use Magento\Client\Rest\MagentoRestClient;

$client = MagentoRestClient::factory(array(
    'base_url'        => 'http://magentohost',
    'consumer_key'    => 'abc123...',
    'consumer_secret' => 'def456...',
    'token'           => 'ghi789...',
    'token_secret'    => 'jkl012...',
));

$result = $client->get('/api/rest/products')->send()->json();
5
  • how can I display my API values as a table format. my post : magento.stackexchange.com/q/209734/57334
    – zus
    Commented Jan 18, 2018 at 5:52
  • I am used to WooCommerce and Shopify where we only need a key and secret, how do we get tokens?
    – HippoDuck
    Commented Jan 23, 2019 at 17:31
  • 1
    You can use a software called Postman to do the first request just to get these tokens once. After that, you won't need to request again. Commented Jan 23, 2019 at 17:42
  • @RafaelCorrêaGomes I just realised I've been watching your YouTube video about authentication. Do you know if I can use the same token forever? My system takes orders every 10 minutes and puts them into a WMS. Can I just use that same token?
    – HippoDuck
    Commented Jan 30, 2019 at 17:42
  • @user2924019 yes, the token can be used forever or until you invalidated it. Commented Jan 30, 2019 at 20:33
0

Here is an example of REST API in magento2

Step 1: Vendor/Demo/etc/webapi.xml

<?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/get-categorie-id" method="GET">
        <service class="Vendor\Demo\Api\CategorieInterface" method="getId"/>
        <resources>
            <resource ref="anonymous"/> 
        </resources>
    </route>
</routes>

Step 2: Vendor/Demo/Api/CategorieInterface.php

<?php
namespace Vendor\Demo\Api;

interface CategorieInterface
{
/**
* GET for inquiry api
* @return int
*/
    public function getId();
}

Step 3: Vendor/Demo/etc/di.xml

<?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="Vendor\Demo\Api\CategorieInterface" type="Vendor\Demo\Model\CategorieRepository"/>

</config>

Step 4: Vendor/Demo/Model/CategorieRepository.php

<?php

namespace Vendor\Demo\Model;

use Vendor\Demo\Api\CategorieInterface;

class CategorieRepository implements CategorieInterface {

    protected $_scopeConfig;

    public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
    {
        $this->_scopeConfig = $scopeConfig;
    }

    public function getId() {
    // Here is your custom logic and return it
        print_r("It's Working");
        die;
    }
}

API endpoint like this:

http://demo.com/rest/V1/get-categorie-id

{{BASE_URL}}/rest//{ROUTE_URL}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.