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.
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.
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.
<?phpclassIndexControllerextendsZend_Controller_Action{publicfunctioninit(){$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');}publicfunctionindexAction(){$accesssession=newZend_Session_Namespace('AccessToken');if(isset($accesssession->accessToken)){$token=unserialize($accesssession->accessToken);// $client = $token->getHttpClient($this->config);
$client=newZend_Http_Client();$adapter=newZend_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=newZend_Oauth_Consumer($this->config);$token=$consumer->getRequestToken();$requestsession=newZend_Session_Namespace('RequestToken');$requestsession->requestToken=serialize($token);$consumer->redirect();}$this->view->products=$products;}publicfunctioncallbackAction(){$requestsession=newZend_Session_Namespace('RequestToken');if(!empty($_GET)&&isset($requestsession->requestToken)){$accesssession=newZend_Session_Namespace('AccessToken');$consumer=newZend_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?
thrownewException('Invalid callback request. Oops. Sorry.');}}publicfunctioncallbackrejectedAction(){// 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
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.
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.
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
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.