Skip to content

Instantly share code, notes, and snippets.

@rafaelstz
Last active December 9, 2019 02:37
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save rafaelstz/4259811d7dae9f34a80898dd68ab2496 to your computer and use it in GitHub Desktop.
Save rafaelstz/4259811d7dae9f34a80898dd68ab2496 to your computer and use it in GitHub Desktop.
Magento 1 - Sample PHP script to connect via API REST
<?php
// Created by Rafael Corrêa Gomes
// Reference http://devdocs.magento.com/guides/m1x/api/rest/introduction.html#RESTAPIIntroduction-RESTResources
// Custom Resource
$apiResources = "products?limit=2";
// Custom Values
$isAdminUser = true;
$adminUrl = "admin";
$callbackUrl = "http://dev.local3.com/rest.php";
$host = 'http://dev.local3.com/magento/';
$consumerKey = '7e1339ba397c917f00066fca13543934';
$consumerSecret = '6a3bc1999d9399c655f0b28edbdb8610';
// Don't change
$temporaryCredentialsRequestUrl = $host . "oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = ($isAdminUser) ? $host . $adminUrl . "/oauth_authorize" : $host . "oauth/authorize";
$accessTokenRequestUrl = $host . "oauth/token";
$apiUrl = $host . "api/rest/";
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.$apiResources;
$oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => '*/*'));
// $productsList = json_decode($oauthClient->getLastResponse());
$productsList = $oauthClient->getLastResponse();
// echo "<pre>";
print_r($productsList);
// echo "</pre>";
}
} catch (OAuthException $e) {
echo "<pre>";
print_r($e->getMessage());
echo "<br/>";
print_r($e->lastResponse);
echo "</pre>";
}
@Msentri
Copy link

Msentri commented Feb 21, 2017

HI please help it gives me an error :

the url is : http://codedev.co.za/sandile/test.php

image

@Noor-mohamad
Copy link

Hii
I have to use magento 1 rest api to the third party
I have used above code but getting this error.

Notice: Use of undefined constant OAUTH_AUTH_TYPE_URI - assumed 'OAUTH_AUTH_TYPE_URI' in D:\xampp\htdocs\apicheck\index.php on line 15

Fatal error: Class 'OAuth' not found in D:\xampp\htdocs\apicheck\index.php on line 16
can you please help me.

@gelanivishal
Copy link

@Noor-mohamad
You need this file php_oauth.dll (windows)

Put into

/bin/php[version]/ext
And you need to change php.ini to load oauth (Dynamic Extensions)

extension=php_oauth.dll

@ramurambabu
Copy link

i followed your tutorial but i got the page is not working error please tell how to solved it

@baljinderimpinge
Copy link

Hello @rafael,

I have followed the same steps but I am getting this error :- https://prnt.sc/oyj32j

Will be able to help me to fix this issue?

I'll be looking forward to your response.

Thanks

@rafaelstz
Copy link
Author

Hi @baljinderimpinge,

I think the issue is related with the route mage1 included in the request.

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