· 7 years ago · Jun 04, 2018, 07:24 PM
1<?php
2// Created by Rafael Corrêa Gomes
3// Reference http://devdocs.magento.com/guides/m1x/api/rest/introduction.html#RESTAPIIntroduction-RESTResources
4
5// RESOURCE QUE QUER BUSCAR. Ex.: products/customer/order...
6$apiResources = "products?limit=5";
7
8// Custom Values
9$isAdminUser = true;
10$adminUrl = "admin0o9i8u";
11
12/*
13URL DO SCRIPT NA MAQUINA LOCAL
14*/
15$callbackUrl = "http://localhost:8888/magerest/mg.php";
16
17// URL DA LOJA
18$host = 'https://www.pormadeonline.com.br/';
19
20$consumerKey = '35a7123bb99f84109b504eb7281e3f08';
21$consumerSecret = 'ee49606f650c9a8fb00f87809198277d';
22
23
24// Don't change
25$temporaryCredentialsRequestUrl = $host . "oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
26$adminAuthorizationUrl = ($isAdminUser) ? $host . $adminUrl . "/oauth_authorize" : $host . "oauth/authorize";
27$accessTokenRequestUrl = $host . "oauth/token";
28$apiUrl = $host . "api/rest/";
29
30session_start();
31if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
32 $_SESSION['state'] = 0;
33}
34try {
35 $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
36 $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
37 $oauthClient->enableDebug();
38
39 if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
40 $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
41 $_SESSION['secret'] = $requestToken['oauth_token_secret'];
42 $_SESSION['state'] = 1;
43 header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
44 exit;
45 } else if ($_SESSION['state'] == 1) {
46 $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
47 $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
48 $_SESSION['state'] = 2;
49 $_SESSION['token'] = $accessToken['oauth_token'];
50 $_SESSION['secret'] = $accessToken['oauth_token_secret'];
51 header('Location: ' . $callbackUrl);
52 exit;
53 } else {
54 $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
55 $resourceUrl = $apiUrl.$apiResources;
56 $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => '*/*'));
57 // $productsList = json_decode($oauthClient->getLastResponse());
58 $productsList = $oauthClient->getLastResponse();
59 // echo "<pre>";
60 print_r(html_entity_decode($productsList));
61 // echo "</pre>";
62 }
63} catch (OAuthException $e) {
64 echo "<pre>";
65 print_r($e->getMessage());
66 echo "<br/>";
67 print_r($e->lastResponse);
68 echo "</pre>";
69}