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
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.
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
Normally you can get the http client by
rather than creating
Zend_Http_Client as I did. But due to some issues
the request was getting
Service Temporarily Unavailable as response.
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.