forked from stuaroo/shiny-affiliate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmazonProductApi.php
181 lines (169 loc) · 5.8 KB
/
AmazonProductApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* Use Amazon Product API to display products and product info from Amazon.com
*
* Uses Amazon's Product Advertising API to display products and product info
* from Amazon using RESTful web requests; can also be used to build remote
* shopping carts to allow users to checkout directly from Amazon.com
* @version 1.2.0
*/
class AmazonProductApi
{
protected $_publicKey = 'xxxxxxxxxxxxxxxxxxxxxx';
protected $_privateKey = 'xxxxxxxxxxxxxxxxxxxxxx';
protected $_associateTag = 'xxxxxxxxxxxxxxxxxxxxxx';
/**
* Get a signed URL
*
* @param array $param used to build url
* @return array $signature returns the signed string and its components
*/
protected function generateSignature($param)
{
$signature['method'] = 'GET';
$signature['host'] = 'webservices.amazon.com';
$signature['uri'] = '/onca/xml';
$param['Service'] = 'AWSECommerceService';
$param['AWSAccessKeyId'] = $this->_publicKey;
$param['AssociateTag'] = $this->_associateTag;
$param['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z");
$param['Version'] = '2011-08-01';
ksort($param);
// URL encode key/value pairs
foreach($param as $key => $value)
{
$key = str_replace("%7E", "~", rawurlencode($key));
$value = str_replace("%7E", "~", rawurlencode($value));
$queryParamsUrl[] = $key . '=' . $value;
}
// add ampersands between key/value pairs
$signature['queryUrl'] = implode("&", $queryParamsUrl);
// start signature creation
$stringToSign = $signature['method'] . "\n" . $signature['host'] . "\n" . $signature['uri'] . "\n" . $signature['queryUrl'];
$signature['string'] = str_replace("%7E", "~", rawurlencode(base64_encode(hash_hmac("sha256", $stringToSign, $this->_privateKey, TRUE))));
return $signature;
}
/**
* Get signed url response
*
* @param array $params
* @return string $signedUrl a query url with signature
*/
public function getSignedUrl($params)
{
$signature = $this->generateSignature($params);
return $signedUrl = "http://" . $signature['host'] . $signature['uri'] . '?' . $signature['queryUrl'] . '&Signature=' . $signature['string'];
}
/**
* Get a signed url for ItemLookup Operation
*
* @param array $items a list of ASINs to lookup
* @param string $responseGroup a comma-separated string with desired response groups, defaults to Offers,Images
* @return string $signedUrl a query url with signature
*/
public function itemLookup($items, $responseGroup = 'Offers,Images')
{
$parameters = array(
"ResponseGroup"=>$responseGroup,
"Operation"=>"ItemLookup",
"MerchantId"=>"Amazon",
"IdType"=>"ASIN"
);
if ( is_array($items) )
{
$parameters['ItemId'] = implode(',', $items);
}
return $signedUrl = $this->getSignedUrl($parameters);
}
/**
* Get a signed url for CartCreate Operation
*
* @param array $offerId an array of OfferListingId's from ItemLookup xml file (or similar); keys should be ints
* @param array $qty the quantity of each offer to add to cart
* @return string $signedUrl a query url with signature
*/
public function cartCreate($offerId, $qty)
{
if ( is_array($offerId) && is_array($qty) )
{
$parameters = array(
"Operation"=>"CartCreate"
);
foreach ( $offerId as $key => $value )
{
$parameters['Item.' . $key . '.OfferListingId'] = $value;
$parameters['Item.' . $key . '.Quantity'] = $qty[$key];
}
return $signedUrl = $this->getSignedUrl($parameters);
}
}
/**
* Get signed url for CartAdd Operation
*
* @param array $offerId an array of OfferListingId's from ItemLookup xml file (or similar); keys should be ints
* @param array $qty the quantity of each offer to add to cart
* @param string $hmac Hash Message Authentication Code returned by CartCreate that identifies a cart
* @param string $cartId Alphanumeric token returned by CartCreate that identifies a cart
* @return string $signedUrl a query url with signature
*/
public function cartAdd($offerId, $qty, $hmac, $cartId)
{
if ( is_array($offerId) && is_array($qty) )
{
$parameters = array(
"Operation"=>"CartAdd",
"HMAC"=>$hmac,
"CartId"=>$cartId
);
foreach ( $offerId as $key => $value )
{
$parameters['Item.' . $key . '.OfferListingId'] = $value;
$parameters['Item.' . $key . '.Quantity'] = $qty[$key];
}
return $signedUrl = $this->getSignedUrl($parameters);
}
}
/**
* Get signed url for CartModify Operation
*
* @param array $cartItemId an array of CartItemId's from CartCreate response xml file; keys should be ints
* @param array $qty the quantity of each offer to change in cart (0 to delete)
* @param string $hmac Hash Message Authentication Code returned by CartCreate that identifies a cart
* @param string $cartId Alphanumeric token returned by CartCreate that identifies a cart
* @return string $signedUrl a query url with signature
*/
public function cartModify($cartItemId, $qty, $hmac, $cartId)
{
if ( is_array($cartItemId) && is_array($qty) )
{
$parameters = array(
"Operation"=>"CartModify",
"HMAC"=>$hmac,
"CartId"=>$cartId
);
foreach ( $cartItemId as $key => $value )
{
$parameters['Item.' . $key . '.CartItemId'] = $value;
$parameters['Item.' . $key . '.Quantity'] = $qty[$key];
}
return $signedUrl = $this->getSignedUrl($parameters);
}
}
/**
* Get signed url for CartGet Operation
*
* @param string $hmac Hash Message Authentication Code returned by CartCreate that identifies a cart
* @param string $cartId Alphanumeric token returned by CartCreate that identifies a cart
* @return string $signedUrl a query url with signature
*/
public function cartGet($hmac, $cartId)
{
$parameters = array(
"Operation"=>"CartGet",
"HMAC"=>$hmac,
"CartId"=>$cartId
);
return $signedUrl = $this->getSignedUrl($parameters);
}
}
?>