· 9 years ago · Dec 02, 2016, 10:30 AM
1<?php
2 session_start();
3?>
4<html>
5 <head>
6 <title>Matt's API</title>
7 <link rel="stylesheet" type="text/css" href="/matt/api.css">
8 </head>
9 <body>
10 Enter a product SKU to get the price
11 <form class="get_price" action="get_product_script.php" method="post">
12 <input type="text" name="product_sku">
13 <input type="submit" class="form_submit">
14 </form>
15 <br><br>
16 </body>
17</html>
18<?php
19 if (isset($_SESSION['query_result'])) {
20 echo $_SESSION['query_result'];
21 unset($_SESSION['query_result']);
22 }
23?>
24
25<link rel="stylesheet" type="text/css" href="/matt/api.css">
26<?php
27 /* Get Product SKU from Form */
28 $product_sku = $_POST['product_sku'];
29
30 /* Variables */
31 $callbackURL = "http://craftingly.hosting/matt/matts_api.php";
32 $temporaryCredentialsRequestURL = "https://ts564737-container.zoeysite.com/oauth/initiate?oauth_callback=" . URLencode($callbackURL);
33 $adminAuthorizationURL = 'https://ts564737-container.zoeysite.com/admin/oauth_authorize';
34 $accessTokenRequestURL = 'https://ts564737-container.zoeysite.com/oauth/token';
35 $URL = 'https://ts564737-container.zoeysite.com';
36 $apiURL = $URL . '/api/rest';
37 $consumerKey = '526ced0202719d14951e1849016d6b3d';
38 $consumerSecret = 'a06606b73962a0efbafea32af3d89380';
39
40 /* Create/Resume Session */
41 session_start();
42
43 if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
44 $_SESSION['state'] = 0;
45 }
46
47 try {
48 /* Variables */
49 $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
50 $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
51 $oauthClient->enableDebug();
52
53 if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
54 $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestURL);
55 $_SESSION['secret'] = $requestToken['oauth_token_secret'];
56 $_SESSION['state'] = 1;
57 header('Location: ' . $adminAuthorizationURL . '?oauth_token=' . $requestToken['oauth_token']);
58 } else if ($_SESSION['state'] == 1) {
59 $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
60 $accessToken = $oauthClient->getAccessToken($accessTokenRequestURL);
61 $_SESSION['state'] = 2;
62 $_SESSION['token'] = $accessToken['oauth_token'];
63 $_SESSION['secret'] = $accessToken['oauth_token_secret'];
64 header('Location: ' . $callbackURL);
65 } else {
66 $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
67 $resourceURL = "$apiURL/products/?order=entity_id&filter[0][attribute]=sku&filter[0][in][0]=" . $product_sku;
68 $oauthClient->fetch($resourceURL, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
69 $productList = json_decode($oauthClient->getLastResponse());
70 }
71 } catch (OAuthException $e) {
72 echo '<pre>';print_r($e);echo '</pre>';
73 }
74
75 /* Get price of the product SKU */
76 if ($productList) {
77 foreach ($productList as $product) {
78 $_SESSION['query_result'] = 'Price of <b>' . $product_sku . '</b> is <span style="color: #ff0000; font-weight: bold;">£' . round($product->price, 2) . '</span>';
79 }
80 } else {
81 $_SESSION['query_result'] = 'Product SKU <b>' . $product_sku . '</b> does not exist in the database.';
82 }
83
84 /* Redirect back to the form page */
85 header('Location: ' . $callbackURL);
86?>