/mvc/components/amazon
[return to app]1
<?php
2 /**
3 * Interface to Amazon Web Services
4 */
5 class amazonComponent {
6 /**
7 * Cache storing the last response from AWS
8 * @var object
9 */
10 public $lastResponse;
11
12 /**
13 * Response group as defined by the AWS API (item types that you want to get back from their web service)
14 * @var array
15 */
16 public $responseGroup = array('ItemAttributes', 'Images');
17
18 /**
19 * Connects to Amazon Web Services to get products by ASIN/ISBN
20 *
21 * @param string $asin
22 * @param boolean $isbn13
23 * @return string XML response
24 */
25 public function getProductByAsin($asin, $isbn13 = false) {
26 $params = array('Service' => 'AWSECommerceService',
27 'ResponseGroup' => implode(',', $this->responseGroup),
28 'AWSAccessKeyId' => get::$config->AMAZON['accessKey'],
29 'Operation' => 'ItemLookup',
30 'AssociateTag' => get::$config->AMAZON['id'],
31 'Timestamp' => gmdate('Y-m-d\TH:i:s') . '.000Z',
32 'Version' => '2009-03-31',
33 'ItemId' => (!is_array($asin) ? $asin : implode(',', $asin)));
34 if ($isbn13) {
35 $params['IdType'] = 'EAN';
36 $params['SearchIndex'] = 'Books';
37 }
38 ksort($params);
39 foreach ($params as $key => $val) {
40 $encoded[] = str_replace('%7E', '~', rawurlencode($key))
41 . '=' . str_replace('%7E', '~', rawurlencode($val));
42 }
43 $request = implode('&', $encoded);
44 $host = 'ecs.amazonaws.com';
45 $path = '/onca/xml';
46 $signature = "GET\n" . $host . "\n" . $path . "\n" . $request;
47 $signature = urlencode(base64_encode(hash_hmac('sha256', $signature, get::$config->AMAZON['secretKey'],
true)));
48 $url = 'http://' . $host . $path . '?' . $request . '&Signature=' . $signature;
49 $this->lastResponse = simplexml_load_string(file_get_contents($url));
50 return $this->lastResponse;
51 }
52
53 /**
54 * Internal sorting function to properly alphabetize the combined results of Amazon Web Services and cache
response
55 *
56 * @param string $a
57 * @param string $b
58 * @return int
59 */
60 protected static function _titleSort($a, $b) {
61 return strcasecmp($a['title'], $b['title']);
62 }
63
64 /**
65 * Pass in one string or an array of ASIN or ISBN and get the items returned back
66 * This is a wrapper method that simplifies the access and return data to $this->getProductByAsin
67 *
68 * @param string $asin
69 * @param boolean $isbn13
70 * @param boolean $getErrors
71 * @return array
72 */
73 public function getTitlesByAsin($asin, $isbn13 = false, $getErrors = false) {
74 $amazon = get::model('amazon');
75 $titles = $amazon->getCache($asin);
76 if (!is_array($asin)) {
77 $asin = (isset($titles[$asin]) ? false : $asin);
78 } else {
79 $asin = array_diff($asin, array_keys($titles));
80 }
81 if ($asin) {
82 $xml = $this->getProductByAsin($asin, $isbn13);
83 if ($xml && property_exists($xml->Items, 'Item')) {
84 foreach ($xml->Items->Item as $item) {
85 $thisAsin = (string) $item->ASIN;
86 $titles[$thisAsin]['title'] = $item->ItemAttributes->Title;
87 $titles[$thisAsin]['url'] = $item->DetailPageURL;
88 $titles[$thisAsin]['thumbnail'] = array('src' => $item->SmallImage->URL,
89 'height' => $item->SmallImage->Height,
90 'width' => $item->SmallImage->Width,
91 'alt' => $titles[$thisAsin]['title']);
92 }
93 }
94 if ($getErrors && property_exists($xml->Items->Request, 'Errors')) {
95 foreach ($xml->Items->Request->Errors->Error as $Error) {
96 if ($Error->Code == 'AWS.InvalidParameterValue') {
97 $asin = substr($Error->Message, 0, strpos($Error->Message, ' '));
98 $titles[(string) $asin]['error'] = $Error->Message;
99 }
100 }
101 } else {
102 $amazon->updateCache($titles);
103 }
104 uasort($titles, array('self', '_titleSort'));
105 }
106 return $titles;
107 }
108 }