Services_Amazon
[ class tree: Services_Amazon ] [ index: Services_Amazon ] [ all elements ]

Source for file amazon_ecs4_cart.php

Documentation is available at amazon_ecs4_cart.php

  1. <?php
  2. //
  3. // Example of usage for Services_AmazonECS4
  4. //
  5. // This example uses the following functions:
  6. // - CartAdd
  7. // - CartClear
  8. // - CartCreate
  9. // - CartGet
  10. // - CartModify
  11. //
  12. // * VERY IMPORTANT *
  13. // YOU NEED TO CHANGE THE SUBSCRIPTION TO SOMETHING OTHER THEN [your Subscription ID here]
  14. // YOU ALSO SHOULD CHANGE THE ASSOSCIATE ID TO YOUR OWN
  15. // * VERY IMPORTANT *
  16.  
  17. require_once 'PEAR.php';
  18. require_once 'Services/AmazonECS4.php';
  19.  
  20. function existsCart()
  21. {
  22.     return isset($_COOKIE['CartId']? true : false;
  23. }
  24.  
  25. $amazon = new Services_AmazonECS4('[your Subscription ID here]''[your Associate ID]');
  26.  
  27. switch ($_GET['action']{
  28. case 'add':
  29.     $item = array('ASIN' => $_GET['ASIN'],
  30.                   'Quantity' => $_GET['Quantity']);
  31.     if (!existsCart()) {
  32.         $result $amazon->CartCreate($item);
  33.         if (PEAR::isError($result)) {
  34.             die($result->message);
  35.         }
  36.         setcookie('CartId'$result['CartId']time(+ 60*60*24);
  37.         setcookie('HMAC'$result['HMAC']time(+ 60*60*24);
  38.     else {
  39.         $result $amazon->CartAdd($_COOKIE['CartId']$_COOKIE['HMAC']$item);
  40.         if (PEAR::isError($result)) {
  41.             die($result->message);
  42.         }
  43.     }
  44.     break;
  45.  
  46. case 'modify':
  47.     if (!existsCart()) {
  48.         die('error');
  49.     }
  50.     $item = array('CartItemId' => $_GET['CartItemId']);
  51.     if (isset($_GET['SaveForLater'])) {
  52.         $item += array('Action' => 'SaveForLater');
  53.     else if (isset($_GET['MoveToCart'])) {
  54.         $item += array('Action' => 'MoveToCart');
  55.     else {
  56.         $item += array('Quantity' => $_GET['Quantity']);
  57.     }
  58.     $result $amazon->CartModify($_COOKIE['CartId']$_COOKIE['HMAC']$item);
  59.     if (PEAR::isError($result)) {
  60.         die($result->message);
  61.     }
  62.     
  63.     break;
  64.  
  65. case 'clear':
  66.     if (!existsCart()) {
  67.         die('error');
  68.     }
  69.     $result $amazon->CartClear($_COOKIE['CartId']$_COOKIE['HMAC']);
  70.     break;
  71.  
  72. default:
  73.     if (existsCart()) {
  74.         $result $amazon->CartGet($_COOKIE['CartId']$_COOKIE['HMAC']);
  75.         if (PEAR::isError($result)) {
  76.             setcookie('CartId'null0);
  77.             setcookie('HMAC'null0);
  78.             die($result->message);
  79.         }
  80.     }
  81.     break;
  82. }
  83.  
  84. echo <<< EOT
  85. <html>
  86. <head>
  87.     <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
  88.     <title>Services_AmazonECS4 example</title>
  89. </head>
  90. <body>
  91. <h1>Amazon ECS4 example - Cart Operations</h1>
  92. <p>
  93.  
  94. </p>
  95. EOT;
  96.  
  97. echo <<< EOT
  98. <form action="{$_SERVER['PHP_SELF']}" method="get">
  99. <table border="0">
  100. <tr>
  101.     <td>ASIN <input type="text" name="ASIN" size="20" /></td>
  102.     <td>Quantity <input type="text" name="Quantity" size="3" value="1" /></td>
  103.     <td><input type="submit" value="Add to cart" /></td>
  104. </tr>
  105. </table>
  106. <input type="hidden" name="action" value="add" />
  107. </form>
  108. EOT;
  109.  
  110. // CartItems
  111. echo <<< EOT
  112. CartItems :<br />
  113. <table border="1">
  114. <tr><th>ASIN</th><th>Title</th><th>Price</th><th>Qty</th><th>Save</th><th></th></tr>
  115. EOT;
  116. $items = array();
  117. if (isset($result)) {
  118.     if (isset($result['CartItems']['CartItem']['CartItemId'])) {
  119.         $items['0'$result['CartItems']['CartItem'];
  120.     else if (isset($result['CartItems']['CartItem'])) {
  121.         $items += $result['CartItems']['CartItem'];
  122.     }
  123. }
  124. foreach ($items as $v{
  125. echo <<< EOT
  126.     <tr>
  127.         <form action="{$_SERVER['PHP_SELF']}" method="get">
  128.         <td>{$v['ASIN']}</td>
  129.         <td>{$v['Title']}</td>
  130.         <td>{$v['Price']['FormattedPrice']}</td>
  131.         <td>
  132.             <input type="text" size="3" name="Quantity" value="{$v['Quantity']}" />
  133.             <input type="hidden" name="CartItemId" value="{$v['CartItemId']}" />
  134.         </td>
  135.         <td>
  136.             <input type="checkbox" name="SaveForLater" value="save" />
  137.         </td>
  138.         <td>
  139.             <input type="submit" value="Update" />
  140.         </td>
  141.         <input type="hidden" name="action" value="modify" />
  142.         </form>
  143.     </tr>
  144. EOT;
  145. }
  146. echo <<< EOT
  147. </table><br />
  148. EOT;
  149.  
  150. // Saved Items
  151. echo <<< EOT
  152. Saved Items to buy later :<br />
  153. <table border="1">
  154. <tr><th>ASIN</th><th>Title</th><th>Price</th><th>Qty</th><th>Move</th><th></th></tr>
  155. EOT;
  156. $items = array();
  157. if (isset($result)) {
  158.     if (isset($result['SavedForLaterItems']['SavedForLaterItem']['CartItemId'])) {
  159.         $items['0'$result['SavedForLaterItems']['SavedForLaterItem'];
  160.     else if (isset($result['SavedForLaterItems']['SavedForLaterItem'])) {
  161.         $items += $result['SavedForLaterItems']['SavedForLaterItem'];
  162.     }
  163. }
  164. foreach ($items as $v{
  165. echo <<< EOT
  166.     <tr>
  167.         <form action="{$_SERVER['PHP_SELF']}" method="get">
  168.         <td>{$v['ASIN']}</td>
  169.         <td>{$v['Title']}</td>
  170.         <td>{$v['Price']['FormattedPrice']}</td>
  171.         <td>
  172.             <input type="text" size="3" name="Quantity" value="{$v['Quantity']}" />
  173.             <input type="hidden" name="CartItemId" value="{$v['CartItemId']}" />
  174.         </td>
  175.         <td>
  176.             <input type="checkbox" name="MoveToCart" value="move" />
  177.         </td>
  178.         <td>
  179.             <input type="submit" value="Update" />
  180.         </td>
  181.         <input type="hidden" name="action" value="modify" />
  182.         </form>
  183.     </tr>
  184. EOT;
  185. }
  186. echo <<< EOT
  187. </table><br />
  188. EOT;
  189.  
  190. // Clear
  191. if (isset($result['CartItems']|| isset($result['SavedForLaterItems'])) {
  192. echo <<< EOT
  193. <p><a href="{$_SERVER['PHP_SELF']}?action=clear">Clear</a></p>
  194. EOT;
  195. }
  196.     
  197. // Purchase
  198. if (isset($result['CartItems']|| isset($result['SavedForLaterItems'])) {
  199. echo <<< EOT
  200. <p><a href="{$result['PurchaseURL']}">Purchase</a></p>
  201. EOT;
  202. }
  203.  
  204. // Processing Time
  205. echo '<p>Processing Time : ' $amazon->getProcessingTime('sec</p>';
  206.  
  207. // Result
  208. echo '<p>Result :';
  209. var_dump($result);
  210. echo '</p>';
  211.  
  212. echo <<< EOT
  213. </body>
  214. </html>
  215. EOT;
  216.  
  217. ?>

Documentation generated on Mon, 11 Mar 2019 14:13:44 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.