2

i am beginner in creating Rest api, i dont know from where to start. till now am able to pull products list using http://my_host.com/api/rest/products. but am confused what will my url for calling custom function

asked Mar 24, 2014 at 6:47
1

2 Answers 2

2

SOLVED:

Tried this article "How to Extend the Magento REST API to Use Coupon Auto Generation"

and with small tweak to call my custom method.

7ochem
7,61516 gold badges54 silver badges82 bronze badges
answered Mar 24, 2014 at 14:04
0
0
<?php
/**
 * Example of retrieving the products list using Admin account via Magento REST API. OAuth authorization is used
 * Preconditions:
 * 1. Install php oauth extension
 * 2. If you were authorized as a Customer before this step, clear browser cookies for 'yourhost'
 * 3. Create at least one product in Magento
 * 4. Configure resource permissions for Admin REST user for retrieving all product data for Admin
 * 5. Create a Consumer
 */
// $callbackUrl is a path to your file with OAuth authentication example for the Admin user
//Server global variables declarations
$baseUrl = $_SERVER['HTTP_HOST'];
$scriptName = $_SERVER['SCRIPT_NAME'];
$protocol = 'http://';
$callbackUrl = $protocol.$baseUrl.$scriptName;
//Variables for oAuth 1.0a
$temporaryCredentialsRequestUrl = $protocol.$baseUrl."/oauth/initiate?oauth_callback=" . $callbackUrl;
$adminAuthorizationUrl = $protocol.$baseUrl.'/admin/oauth_authorize';
$accessTokenRequestUrl = $protocol.$baseUrl.'/oauth/token';
$resourceUrl = 'products';
$apiUrl = $protocol.$baseUrl.'/api/rest';
$consumerKey = 'Your Consumer Key';
$consumerSecret = 'Your Consumer Secret';
if(isset($resourceUrl) && !empty($resourceUrl)) {
 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/$resourceUrl";
 $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json'));
 $productsList = json_encode($oauthClient->getLastResponse());
 echo $productsList;
 }
 } catch (OAuthException $e) {
 print_r($e->getMessage());
 echo "&lt;br/&gt;";
 print_r($e->lastResponse);
 }
} else {
 echo "Resource url is empty." . "<br />";
 echo "Please enter resource url:" . "<br />";
 echo "e.g. ?resource=products" . "<br />";
}`enter code here`
?>
answered Nov 29, 2016 at 14:14
2
  • You can also get resource url as a parameter in the link for making different rest api calls froma single PHP calling script: $resourceUrl = isset($_GET['resource']) ? $_GET['resource'] : NULL; Commented Nov 29, 2016 at 14:18
  • How to configure Magento REST and oAuth settings Steps: 1. Creating oAuth Consumer 2. Creating and configuring Admin roles 3. Assigning configured Admin REST Role to admin user 4. Configuring resource attributes and access permissions drive.google.com/file/d/0Bxww_nvHTw3yMVdHTE5oUEdBRDg/… Commented Nov 29, 2016 at 14:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.