Magento 1.9 – How to Retrieve Products from REST API

magento-1.9magento-communityrest

I'm new to the REST api concept, after hitting the url- http://magentohost/api/rest/products I can see the products, means can access the product resource through guest user.

Then I have created the consumer from the admin panel and get the consumer key and secret then added new admin role and from REST attributes gave all resouce access to admin and linked admin user to REST role. Then I put sample php example for retrieving the products from documentation outside the magento root directory filled the url paths and consumer key and secret and created new file which is call back url.

Now when i run this sample file it works fine, ask user to fill his credentials, authorize the application and sent back to callback url.

There is nothing in the call back page, i have kept it blank, so what should i do to retrieve the list of products ? Is there anything that I'm missing ? How should I move further to learn REST API's ? Thanks in advance

Best Answer

$url = 'magentohost url';
$callbackUrl = $url . "oauth_admin.php";
$temporaryCredentialsRequestUrl = $url . "oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = $url . 'admin/oauth_authorize';
$accessTokenRequestUrl = $url . 'oauth/token';
$apiUrl = $url . 'api/rest';
$consumerKey = 'consumer_key';
$consumerSecret = 'consumer_secret';
$token = 'token';
$secret = 'token_secret';

try {
    $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
    $oauthClient->setToken($token, $secret);
    $resourceUrl = "$apiUrl/products";
    $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
    $productsList = json_decode($oauthClient->getLastResponse());
    echo '<pre>';
    print_r($productsList);
}
catch(Exception $e) {
    echo '<pre>';
    print_r($e);
}