Magento – Rest API with Magento using PHP with example.

apimagento-1.9web services

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 🙂

Best Answer

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();