Skip to content

Instantly share code, notes, and snippets.

@rafaelstz
Last active December 9, 2019 02:37
Show Gist options
  • Select an option

  • Save rafaelstz/4259811d7dae9f34a80898dd68ab2496 to your computer and use it in GitHub Desktop.

Select an option

Save rafaelstz/4259811d7dae9f34a80898dd68ab2496 to your computer and use it in GitHub Desktop.

Revisions

  1. rafaelstz revised this gist Aug 31, 2019. No changes.
  2. rafaelstz revised this gist Nov 29, 2016. No changes.
  3. rafaelstz revised this gist Nov 29, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions magento1-rest.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?php
    // References
    // http://devdocs.magento.com/guides/m1x/api/rest/introduction.html#RESTAPIIntroduction-RESTResources
    // 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";
  4. rafaelstz revised this gist Nov 29, 2016. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions magento1-rest.php
    Original file line number Diff line number Diff line change
    @@ -3,16 +3,16 @@
    // http://devdocs.magento.com/guides/m1x/api/rest/introduction.html#RESTAPIIntroduction-RESTResources

    // Custom Resource
    $apiResources = "products";
    $apiResources = "products?limit=2";

    // Custom Values
    $isAdminUser = true;
    $adminUrl = "admin";
    $callbackUrl = "http://dev.local:88/magento/rest.php";
    $host = 'http://dev.local/magento/';
    $callbackUrl = "http://dev.local3.com/rest.php";
    $host = 'http://dev.local3.com/magento/';

    $consumerKey = 'c523223db1f228a9bdeecdf1410394d9';
    $consumerSecret = '84dc5f0242c7473ec7135c1a5611673a';
    $consumerKey = '7e1339ba397c917f00066fca13543934';
    $consumerSecret = '6a3bc1999d9399c655f0b28edbdb8610';


    // Don't change
  5. rafaelstz revised this gist Nov 28, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions magento1-rest.php
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,8 @@

    // Custom Resource
    $apiResources = "products";

    // Custom Values
    $isAdminUser = true;
    $adminUrl = "admin";
    $callbackUrl = "http://dev.local:88/magento/rest.php";
    @@ -12,6 +14,8 @@
    $consumerKey = 'c523223db1f228a9bdeecdf1410394d9';
    $consumerSecret = '84dc5f0242c7473ec7135c1a5611673a';


    // Don't change
    $temporaryCredentialsRequestUrl = $host . "oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
    $adminAuthorizationUrl = ($isAdminUser) ? $host . $adminUrl . "/oauth_authorize" : $host . "oauth/authorize";
    $accessTokenRequestUrl = $host . "oauth/token";
  6. rafaelstz revised this gist Nov 17, 2016. No changes.
  7. rafaelstz created this gist Nov 17, 2016.
    59 changes: 59 additions & 0 deletions magento1-rest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    <?php
    // References
    // http://devdocs.magento.com/guides/m1x/api/rest/introduction.html#RESTAPIIntroduction-RESTResources

    // Custom Resource
    $apiResources = "products";
    $isAdminUser = true;
    $adminUrl = "admin";
    $callbackUrl = "http://dev.local:88/magento/rest.php";
    $host = 'http://dev.local/magento/';

    $consumerKey = 'c523223db1f228a9bdeecdf1410394d9';
    $consumerSecret = '84dc5f0242c7473ec7135c1a5611673a';

    $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>";
    }