-
Notifications
You must be signed in to change notification settings - Fork 0
/
Amazon.php
314 lines (278 loc) · 10.7 KB
/
Amazon.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
namespace Amazon;
class AmazonException extends \Exception
{
/**
* @todo Error/Exception msg
* @var string
*/
protected $strMsg;
function __construct($argStrNotice,$argStrSevereity=0, $argPrevious = NULL)
{
parent::__construct($argStrNotice, $argStrSevereity, $argPrevious);
$this->strMsg = $argStrNotice;
}
public function __toString ()
{
return $this->strMsg;
}
public function GetMsg ()
{
return $this->strMsg;
}
} // class AmazonException extends \Exception end
interface iAmazon
{
/**
* @todo This function seting up the country details: code, TLD, URL
* @param string $argCC the country code
*
* @return boolean
*/
public function setCountry($argCC);
} // interface iAmazon end
abstract class setAmazon implements iAmazon
{
/**
* @todo the country code store (hide and protect) setable
* @access private
* @var string
*/
private $strCountryCode = 'GB';
/**
* @todo the country TLD store (hide and protect)
* @access private
* @var string
*/
private $strTLD = 'co.uk';
/**
* @todo Amazon URL pattern: http://www.amazon.{COUNTRY_TLD}/dp/{ASIN} (hide and protect)
* {ASIN} will be joined by getItem(),
* at the moment no setable from outside
* @access private
* @var string
*/
private $strAmazonURL = 'http://www.amazon.%1$s/xml/dp/';
/**
* some things which is can setup automaticaly (though it shoud be better from any DBase)
* @todo TLD match
* at the moment no setable from outside
* @access private
* @var array
*/
private $arrTLDMatch = array(
'GB' => 'co.uk',
'US' => 'com',
);
/**
* @todo Query params: Title, price, img tag name in the HTML. setable
* @access private
* @var array
*/
private $arrQParamsFromHTML = array(
'title' => array(
'root' => '//*/span[@id="productTitle"]',
'attributes' => false),
'price' => array(
'root' => '//*/div[@id="price"]//span[@id="priceblock_ourprice"]',
'attributes' => false),
'imageUrl' => array(
'root' => '//*/img[@id="landingImage"]',
'attributes' => 'src'),
);
/**
* @todo some protected and private variable is availebled (just read, if it is necessary).
* If it is not exists it is droped standard error
* @param mixed $argName
* @return mixed
*/
public function __get($argName)
{
if( isset($this->{$argName}) ) { return $this->{$argName}; }
} // public function __get($argName){ end
/**
* @todo some varriable is setable and it can verified (more check/validity)
* setable var(s):
* strCountryCode or construct parameter name: iso3166_1Alpha2CountryCode
* otherwise PHP standard error
* @param string $argVarName class varriable name
* @param mixed $argVarValue varriable value
*/
public function __set($argVarName,$argVarValue)
{
switch("$argVarName")
{
// constructor param name
case 'iso3166_1Alpha2CountryCode':
// field name
case 'strCountryCode':
if( $this->setCountry($argVarValue) ) { return true; }
break;
// case 'strCountryCode': end
// Query parameter setting
case 'arrQParamsFromHTML':
if( is_array($argVarValue) )
{
$this->arrQParamsFromHTML = $argVarValue;
}
break;
// case 'arrQParamsFromHTML': end
} // switch("$argVarName") { end
} // public function __set() { end
/**
* @todo This function seting up the country details: code, TLD, URL
* @param string $argCC the country code
*
* @return boolean
*/
public function setCountry($argCC)
{
if( isset($this->arrTLDMatch[$argCC]) )
{
$this->strCountryCode = $argCC;
$this->strTLD = $this->arrTLDMatch[$argCC];
$this->strURL = sprintf($this->strAmazonURL,$this->strTLD);
return true;
} // if( isset($this->arrTLDMatch[$argVarValue]) ) { end
return false;
} // public function setCountry($argCC) { end
/**
* @todo cat some annoying things from the html
* tags: html, head, body, style, link, meta, javascript, comment,
* map, header, hr, form, input, a, p, 2 or more whitespace
* @param string $argStr HTML string
* @return string
*/
public function clearHtml($argStr)
{
$ret = preg_replace(
array(
"/(<!.*>)/Uis",
"/(<html.*>)/Uis",
"/<head>(.*)<\/head>/Uis",
"/(<script.*>)(.*)<\/script>/Uis",
"/(<style.*>)(.*)<\/style>/Uis",
"/(<link.*>)/Uis",
"/(<meta.*>)/Uis",
"/(<body.*>)/Uis",
"/(<noscript.*>)(.*)<\/noscript>/Uis",
"/(<map.*>)(.*)<\/map>/Uis",
"/(<header.*>)(.*)<\/header>/Uis",
"/(<hr.*>)/Uis",
"/(<hr.*>)(.*)<\/hr>/Uis",
"/(<form.*>)(.*)<\/form>/Uis",
"/(<input.*>)/Uis",
"/(<a.*>)/Uis",
"/(<\/a>)/Uis",
"/(<p.*>)(.*)<\/p>/Uis",
"/({.*})/Uis",
"/( ){2,}/Uis",
"/([ ]){2,}/Uis",
"/([\t])/Uis",
"/(?:\r){2,}/Uis",
"/(?:\n){2,}/Uis",
"/^(?:\r)$/Uis",
"/^(?:\n)$/Uis",
"/^(?:\r\n)$/Uis",
"/(<\/body>)/Uis",
"/(<\/html>)/Uis",
),'',$argStr);
return $ret;
} // public static function clearHtml($argStr) end
} // abstract class setAmazon { end
class Amazon extends setAmazon
{
/**
* @todo this is the exactly URL to amazon
* @var string
*/
protected $strURL;
public function __construct($iso3166_1Alpha2CountryCode)
{
$this->setCountry(strtoupper($iso3166_1Alpha2CountryCode));
} // public function __construct($iso3166_1Alpha2CountryCode) end
/**
* @todo Provided ->getItem($asin) method should return an array containing title,
* price and imageUrl for the requested product or throw an exception if data
* can't be returned e.g. product doesn't exists, is out of stock etc.
* @param string $asin
*
* @return array/exeption
*/
public function getItem($asin)
{
$ret = array();
// XML
$objXPath = $this->_getHtmlStringFromUrl($this->strURL.$asin);
if( !($objXPath instanceof \DOMXPath) )
{
throw new AmazonException('Missing Amazon xml!');
}
// availability
$tags = $objXPath->query('//*/div[@id="availability"]//span[@class="a-size-medium a-color-success"]');
if( $tags instanceof \DOMNodeList && $tags->length>0 )
{
foreach ($tags as $tag)
{
if( trim($tag->nodeValue)!='In stock.' )
{
throw new AmazonException('Out of stock!');
}
} // foreach ($tags as $tag) { end
} // if( $tags ) { end
else
{
throw new AmazonException('No Item!');
}
// title; price; img, etc.
foreach( $this->arrQParamsFromHTML as $key => $arrParamAtt )
{
// parameter root load
$tags = $objXPath->query($arrParamAtt['root']);
if( $tags instanceof \DOMNodeList && $tags->length>0 )
{
foreach ($tags as $tag)
{
if( !$arrParamAtt['attributes'] )
{
$ret[$key][] = $tag->nodeValue;
}
else
{
$ret[$key][] = $tag->getAttribute ( $arrParamAtt['attributes'] );
}
} // foreach ($tags as $tag) { end
} // if( $tags ) { end
else
{
throw new AmazonException('No Item Parameter: '.$key);
}
} // foreach( $this->arrQParamsFromHTML as $key => $arrHTMLIdValue ) { end
return $ret;
} // public function getItem($asin) end
protected function _getHtmlStringFromUrl($url)
{
$rC = curl_init();
curl_setopt($rC, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
curl_setopt($rC, CURLOPT_URL, $url);
curl_setopt($rC, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($rC);
if( $res )
{
curl_close($rC);
$ret = $this->clearHtml($res);
return $this->_getDOMXpathFromHtmlString($ret);
}
else
{
throw new AmazonException('Amazon connection faild!');
}
} // protected function _getHtmlStringFromUrl($url) end
protected function _getDOMXpathFromHtmlString($htmlString)
{
$domDoc = new \DOMDocument();
@$domDoc->loadHtml('<?xml encoding="UTF-8">' .$htmlString);
return new \DOMXPath( $domDoc );
} // protected function _getDOMXpathFromHtmlString($htmlString) end
} // class Amazon extends setAmazon end
?>