Skip to content

Instantly share code, notes, and snippets.

@aschroder
Created April 23, 2012 07:17
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save aschroder/2469319 to your computer and use it in GitHub Desktop.
Save aschroder/2469319 to your computer and use it in GitHub Desktop.
Magento REST API PHP test harness (admin and customer)
<?php
/**
* Copyright Magento 2012
* Example of products list retrieve using Customer account via Magento
REST API. OAuth authorization is used
*/
$callbackUrl = "http://yourhost/oauth_customer.php";
$temporaryCredentialsRequestUrl =
"http://magentohost/oauth/initiate?oauth_callback=" .
urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://magentohost/oauth/authorize';
$accessTokenRequestUrl = 'http://magentohost/oauth/token';
$apiUrl = 'http://magentohost/api/rest';
$consumerKey = 'yourconsumerkey';
$consumerSecret = 'yourconsumersecret';
session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) &&
$_SESSION['state'] == 1) {
$_SESSION['state'] = 0;
}
try {
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION
: OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret,
OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$requestToken =
$oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
$_SESSION['secret'] = $requestToken['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' .
$requestToken['oauth_token']);
exit;
} else if ($_SESSION['state'] == 1) {
$oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
$accessToken =
$oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
header('Location: ' . $callbackUrl);
exit;
} else {
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = "$apiUrl/products";
$oauthClient->fetch($resourceUrl);
$productsList = json_decode($oauthClient->getLastResponse());
print_r($productsList);
}
} catch (OAuthException $e) {
print_r($e);
}
<?php
/**
* Example of products list retrieve using admin account via Magento REST
API. oAuth authorization is used
*/
$callbackUrl = "http://yourhost/oauth_admin.php";
$temporaryCredentialsRequestUrl =
"http://magentohost/oauth/initiate?oauth_callback=" .
urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://magentohost/admin/oAuth_authorize';
$accessTokenRequestUrl = 'http://magentohost/oauth/token';
$apiUrl = 'http://magentohost/api/rest';
$consumerKey = 'yourconsumerkey';
$consumerSecret = 'yourconsumersecret';
session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) &&
$_SESSION['state'] == 1) {
$_SESSION['state'] = 0;
}
try {
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION
: OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret,
OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$requestToken =
$oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
$_SESSION['secret'] = $requestToken['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' .
$requestToken['oauth_token']);
exit;
} else if ($_SESSION['state'] == 1) {
$oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
$accessToken =
$oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
header('Location: ' . $callbackUrl);
exit;
} else {
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = "$apiUrl/products";
$oauthClient->fetch($resourceUrl);
$productsList = json_decode($oauthClient->getLastResponse());
print_r($productsList);
}
} catch (OAuthException $e) {
print_r($e);
}
@Danisan
Copy link

Danisan commented Nov 21, 2013

Hi, I have generated a consumer key and consumer secret for my application. I am looking for a not user interactive method of obtaining an access token, So I won't use urls (callback, etc).. how could this be? is there a method I can call with admin username and admin password, and obtain something like a session id?

@tesmojones
Copy link

Hi, can i get user_id user that login via rest api ?

@rvpatel
Copy link

rvpatel commented Mar 17, 2015

I have getting product list but i wants a with parameter filter like status:1 and visiblity:4 how to add hear:

$resourceUrl = "$apiUrl/products/?page=1&limit=5";
$oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => '/'));

@waqas740
Copy link

waqas740 commented Jul 5, 2015

where did above code is Pasted in magento folder in local server ?

@hatimox
Copy link

hatimox commented Jan 13, 2016

hello i tried to make a test to connect to my magento shop using this code from my local server , but i get Server error 500 from the callbackUrl can you help ?

@mabez
Copy link

mabez commented May 15, 2016

I have a doubt about the class Oauth and the constants mentioned in your example (OAUTH_AUTH_TYPE_AUTHORIZATION, OAUTH_AUTH_TYPE_URI, OAUTH_SIG_METHOD_HMACSHA1, ...).
I know the same constants and class is at the magento doc.
But it don't works for me.
How do you include the Oauth class?
How do you define the constants?

@deepakmalhotra633
Copy link

where did above code is Pasted in magento folder in local server ?

@sanganagouda1
Copy link

how to run this please share me
i am new to magento

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment