Skip to main content

Magento and the REST API

·712 words·4 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones
Table of Contents

Magento provides both REST and SOAP api. In this post I would like to concentrate on REST api to connect and get the products from magento shop.

First we want to register an Oauth consumer to get the consumer key and secret key.

Login to the admin of the magento shop and from the menu System->Web Services->REST - Oauth Consumers and add a new Ouath consumer.

REST - Oauth Consumers

You need the Key and Secret which is what we pass in the config to Zend_Oauth_Consumer shortly.

Let us also assign the right permissions for the attributes. From the menu System->Web Services->REST - Attributes click on Admin and assign the right permission. In our case we give access to products.

REST - Attributes

Now it is time to show you some code. In most case, we would have defined a named route, and depeding upon your route in zend framework call the url.

Assuming the default url http://localhost:8000/index/index and accessing this in your browser will redirect you to the magento shop and get the access token after you authenticating and allowing it.

Note : Make sure that the domain you have logged in and the one you have provided in the callback url of magento REST Oauth consumer are same. Else you will find hard time debugging why session is lost. (I am talking about the difference in http://www.example.com and http://example.com)

You want to change the below parameters in the controller according to yours.

$this->hostname: The magento shop

$consumerKey: The key you got when creating the oauth consumer

$consumerSecret: The secret key you got when creating the oauth consumer

$callbackUrl: The url where it will be redirected to get the accesstoken, same as the one you have provided when creating the oauth consumer.

Source Code
#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        $this->hostname = 'the-magento-host';
        $consumerKey = '';
        $consumerSecret = '';
        $callbackUrl = 'callback-url';
        $this->config = array(
            'callbackUrl' => $callbackUrl,
            'requestTokenUrl' => $this->hostname . '/oauth/initiate',
            'siteUrl' => $this->hostname . '/oauth',
            'consumerKey' => $consumerKey,
            'consumerSecret' => $consumerSecret,
            'authorizeUrl' => $this->hostname . '/admin/oauth_authorize',
            // 'authorizeUrl' => $this->hostname . '/oauth/authorize',
            'accessTokenUrl' => $this->hostname . '/oauth/token'
        );
    }

    public function indexAction()
    {        
        $accesssession = new Zend_Session_Namespace('AccessToken');
        if (isset($accesssession->accessToken)) {
            $token = unserialize($accesssession->accessToken);            
            // $client = $token->getHttpClient($this->config);
            $client = new Zend_Http_Client();
            $adapter = new Zend_Http_Client_Adapter_Curl();
            $client->setAdapter($adapter);
            $adapter->setConfig(array(
                'adapter'   => 'Zend_Http_Client_Adapter_Curl',
                'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
            ));
            $client->setUri($this->hostname . '/api/rest/products');
            $client->setParameterGet('oauth_token', $token->getToken());
            $client->setParameterGet('oauth_token_secret', $token->getTokenSecret());
            $response = $client->request('GET');
            $products = Zend_Json::decode($response->getBody());
        } else {
            $consumer = new Zend_Oauth_Consumer($this->config);
            $token = $consumer->getRequestToken();
            $requestsession = new Zend_Session_Namespace('RequestToken');
            $requestsession->requestToken = serialize($token);
            $consumer->redirect();
        }
        $this->view->products = $products;
    }
    
    public function callbackAction()
    {
        $requestsession = new Zend_Session_Namespace('RequestToken');
        if (!empty($_GET) && isset($requestsession->requestToken)) {
            $accesssession = new Zend_Session_Namespace('AccessToken');
            $consumer = new Zend_Oauth_Consumer($this->config);
            $token = $consumer->getAccessToken(
                $_GET,
                unserialize($requestsession->requestToken)
            );
            $accesssession->accessToken = serialize($token);
            // Now that we have an Access Token, we can discard the Request Token
            unset($requestsession->requestToken);
            // $this->_redirect();
            $this->_forward('index', 'index', 'default');
        } else {
            // Mistaken request? Some malfeasant trying something?
            throw new Exception('Invalid callback request. Oops. Sorry.');
        }
    }
    
    public function callbackrejectedAction()
    {
        // rejected
    }
}

Normally you can get the http client by $token->getHttpClient($this->config) rather than creating Zend_Http_Client as I did. But due to some issues the request was getting Service Temporarily Unavailable as response.

The Service Temporarily Unavailable can also be due to permission issues. You should also check whether you have given the right permissions for roles and resources.

In most case, we would have defined a named route, so you could use the url helpers to generate the route. I hope this helps you to move with REST and Magento

Resources
#

Magento REST API

zend oauth introduction

Thanks
#

I would like to thank PHP Cloud for providing the free service to deploy the magento shop and test it. Installing and deploying app was super simple. So if you have not tried it is time for it.

Related

Aura.Web: A Page Controller for MVC

·52 words·1 min
If you are new to the concept of MVC, or if you are planning to build your own framework, probably Aura.Web help you to do the same. Here is the article in phpmaster.com Aura.Web: Aura’s Page Controller for MVC. Probably you may also like the article Web Routing in PHP with Aura.Router.

Aura System Released Beta 5

·99 words·1 min
Yesterday Paul M Jones tagged the beta5 for the system repo. So you can download the system from http://auraphp.com/system/downloads/ via composer # Creating aura framework based projects has been made easy with composer. You can run php composer.phar create-project -s dev aura/system your-directory What it does is almost similar to git clone and installing via git as below

Standalone Forms for PHP

·710 words·4 mins
Yesterday I wrote about Standalone Forms and Validation. It may need a little bit knowledge on how the Aura.Filter works. But that is a good start if you want to know how you can integrate the pieces of Aura. Today I think I need to show you the very minimal approach you can take to build the form and validate your form. If you want to use a powerful validation and filtering use something like Aura.Filter or bind your favourite components which does the validation Respect, Symfony2 Validator, valitron and filtering with something like DMS-Filter.