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