Mercurial > hg > ywww
comparison xml/getAmazonInfo.php @ 4:a6b55ae70617
original
author | robert |
---|---|
date | Wed, 28 Dec 2016 13:04:53 +0000 |
parents | |
children | 55445b456ad0 |
comparison
equal
deleted
inserted
replaced
3:a5e5475bf357 | 4:a6b55ae70617 |
---|---|
1 <?php | |
2 | |
3 function getAmazonDet($isbn,$go,$localeIn) | |
4 { | |
5 global $output; | |
6 $Adefault=array( | |
7 'language' =>'en', //what language to render the page in | |
8 'locale' =>$localeIn, //which server's products? available: ca,de,fr,jp,uk,us | |
9 //'mode' =>'books', //what product category? | |
10 'page' =>1, //first page to show (we are counting from 1 not 0) | |
11 //'search' =>'Machiavelli', //what to search for? | |
12 'operation' =>'ItemLookup', //what to do? //ItemSearch | |
13 // 'searchindex' =>'Books', //what product category for search? | |
14 'searchparameter' =>'ItemId', //what kind of search? | |
15 'searchparameterdata'=>$isbn, //what to search for? | |
16 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true' | |
17 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending | |
18 'show_url' =>false, //debug: show XML request url to be send to Amazon? | |
19 'show_xml' =>false, //debug: show incoming XML code from Amazon? | |
20 ); | |
21 //change the debug options to true if you want to activate them or call the script with '?show_array=true' to see what actual information you're getting from Amazon and how little my standard script is actually showing of it | |
22 | |
23 $Aassociates_id=array( | |
24 'uk' => 'bookwhack-21', | |
25 'us' => 'your02b-20', | |
26 'ca' => 'book009-20', | |
27 'de' => 'book04c-21', | |
28 'fr' => 'book07f-21', | |
29 ); | |
30 | |
31 $Aserver=array( | |
32 'ca' => array( | |
33 'ext' => 'ca' , //Canadian normal server | |
34 'nor' => 'http://www.amazon.ca' , //Canadian normal server | |
35 'xml' => 'http://xml.amazon.com' , //Canadian xml server | |
36 ), | |
37 'de' => array( | |
38 'ext' => 'de' , //German normal server | |
39 'nor' => 'http://www.amazon.de' , //German normal server | |
40 'xml' => 'http://xml-eu.amazon.com', //German xml server | |
41 ), | |
42 'fr' => array( | |
43 'ext' => 'fr' , //French normal server | |
44 'nor' => 'http://www.amazon.fr' , //French normal server | |
45 'xml' => 'http://xml-eu.amazon.com', //French xml server | |
46 ), | |
47 'jp' => array( | |
48 'ext' => 'jp' , //Japanese normal server, not co.jp! | |
49 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server | |
50 'xml' => 'http://xml.amazon.com' , //Japanese xml server | |
51 ), | |
52 'uk' => array( | |
53 'ext' => 'co.uk' , //UK normal server | |
54 'nor' => 'http://www.amazon.co.uk' , //UK normal server | |
55 'xml' => 'http://xml-eu.amazon.com', //UK xml server | |
56 ), | |
57 'us' => array( | |
58 'ext' => 'com' , //USA normal server | |
59 'nor' => 'http://www.amazon.com' , //USA normal server | |
60 'xml' => 'http://xml.amazon.com' , //USA xml server | |
61 ), | |
62 ); | |
63 | |
64 //if(go != 1) | |
65 //include "aws_signed_request.php"; | |
66 | |
67 $public_key ="AKIAJBXEHTNCU6LLFNIA"; | |
68 $private_key="Dgyv7aR6uGe3OtY95Dj6hHpDS/UEtyboMWpJchYA"; | |
69 | |
70 //for all parameters see if the user has overruled it or use the default | |
71 foreach ($Adefault as $i=>$d) { | |
72 $$i=isset($_GET[$i])?$_GET[$i]:$d; | |
73 } | |
74 //this is the data that is used to form the request for AWS | |
75 //this is the part that is search specific | |
76 $parameters=array( | |
77 'Operation' =>$operation , | |
78 //'Keywords' =>urlencode($search) , | |
79 //'SearchIndex' =>$searchindex , //Books for example. | |
80 "$searchparameter"=>$searchparameterdata , | |
81 'ItemPage' =>$page , //which page? | |
82 'AssociateTag' =>$Aassociates_id[$locale], | |
83 'ResponseGroup' =>'ItemAttributes,Reviews,EditorialReview,OfferSummary,Offers,Images,AlternateVersions,SalesRank,BrowseNodes' , //Small, Medium, Large or SellerListing,'BrowseNodes',// , | |
84 'ReviewSort' =>'-HelpfulVotes' | |
85 ); | |
86 | |
87 $ext=$Aserver[$locale]['ext']; | |
88 $file_data=$ext; | |
89 ksort($parameters); | |
90 foreach ($parameters as $i=>$d) { | |
91 $file_data.='&'.$i.'='.$d; | |
92 } | |
93 | |
94 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); | |
95 //echo $url; | |
96 file_put_contents('/var/ywww/debug/phpDebug',"parms: ". | |
97 print_r($parameters,TRUE)."\n",FILE_APPEND); | |
98 $crl = curl_init(); | |
99 $timeout = 5; | |
100 curl_setopt ($crl, CURLOPT_URL,$url); | |
101 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); | |
102 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | |
103 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | |
104 $output = curl_exec($crl); | |
105 curl_close($crl); | |
106 | |
107 /*$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><ItemLookupErrorResponse xmlns=\"http://ecs.amazonaws.com/doc/2009-03-31/\"><Error><Code>AccountLimitExceeded</Code><Message>Account limit of 2056 requests per hour exceeded.</Message></Error><RequestID>290ed059-730c-4789-93b4-6d21e11053d3</RequestID></ItemLookupErrorResponse>");*/ | |
108 $xml = new SimpleXMLElement($output); | |
109 $review = ""; | |
110 set_error_handler(function () { | |
111 global $output; | |
112 file_put_contents('/var/ywww/debug/phpDebug', "Caught one: ",FILE_APPEND); | |
113 file_put_contents('/var/ywww/debug/phpDebug', | |
114 print_r($output, TRUE)."\n", | |
115 FILE_APPEND); | |
116 } ); | |
117 $review = $xml->Items->Item->CustomerReviews->IFrameURL; | |
118 // The above is failing repeatedly -- | |
119 //PHP Notice: Trying to get property of non-object in | |
120 // /var/ywww/xml/getAmazonInfo.php on line [109] | |
121 // See the dumped structure at the end of this file for the | |
122 // cause | |
123 restore_error_handler(); | |
124 //echo $review; | |
125 $review1 = ""; | |
126 $review2 = ""; | |
127 $review3 = ""; | |
128 if ($review != "") | |
129 { | |
130 $text = @file_get_contents($review . "&truncate=300"); | |
131 $removeTop1 = preg_replace('~<div class="crIFrameHeaderLeftColumn">(.*?)<div class="crIFrameHeaderHistogram">~si', '', $text); | |
132 $removeTop2 = preg_replace('~<div class="crIFrameHeaderHistogram">(.*?)<div class="crIframeReviewList">~si', '', $text); | |
133 if (preg_match('~<body[^>]*>(.*?)</body>~si', $removeTop2, $body)){ $getBody = trim($body[1]); } | |
134 $removeDiv = preg_replace('~<div[^>]*>(.*?)</div>~si', '', $getBody); | |
135 $removeCloseDivs = preg_replace('/<\/div>/','', $removeDiv); | |
136 $setBoundary = str_replace('<!-- BOUNDARY -->','BOTTOM-TOP', $removeCloseDivs); | |
137 //replace <!-- BOUNDARY --> with BOTTOM-TOP | |
138 $remove1 = '~<table cellpadding="0"(.*?)%">~s'; | |
139 $setBoundary = preg_replace($remove1,'', $setBoundary); | |
140 $remove2 = '~</td><td bg(.*?)</table>~s'; | |
141 $setBoundary = preg_replace($remove2,'', $setBoundary); | |
142 $remove3 = '~<a name=(.*?)</a>~s'; | |
143 $setBoundary = preg_replace($remove3,'', $setBoundary); | |
144 $setBoundary2 = str_replace('<br />','', $setBoundary); | |
145 //remove all extra crap; | |
146 $setBoundary3 = str_replace('</td>','BOTTOM', $setBoundary2); | |
147 //replace </td> with BOTTOM | |
148 | |
149 if (preg_match_all('~TOP(.*?)BOTTOM~s', $setBoundary3, $reviews)) | |
150 { | |
151 $reviewContents = $reviews[1]; | |
152 //print_r($reviewContents); | |
153 $review1 = trim($reviewContents[0]); | |
154 $review1 = str_replace("\n", "", $review1); | |
155 $review1 = str_replace("\r", "", $review1); | |
156 if (isset($reviewContents[1])) { | |
157 $review2 = trim($reviewContents[1]); | |
158 $review2 = str_replace("\n", "", $review2); | |
159 $review2 = str_replace("\r", "", $review2); | |
160 } | |
161 else { | |
162 $review2 = ""; | |
163 } | |
164 if (isset($reviewContents[2])) { | |
165 $review3 = trim($reviewContents[2]); | |
166 $review3 = str_replace("\n", "", $review3); | |
167 $review3 = str_replace("\r", "", $review3); | |
168 } | |
169 else { | |
170 $review3 = ""; | |
171 } | |
172 } | |
173 else | |
174 { | |
175 $review1 = ""; | |
176 $review2 = ""; | |
177 $review3 = ""; | |
178 //echo "EPIC FAIL"; | |
179 } | |
180 | |
181 unset($xml->Items->Item->CustomerReviews); | |
182 $xdoc = new DomDocument; | |
183 $xdoc->loadXML($xml->asXML()); | |
184 | |
185 $cReviews = $xdoc ->createElement('CustomerReviews'); | |
186 $cReviewHolder = $xdoc ->createElement('Review'); | |
187 $cReview = $xdoc ->createElement('Content'); | |
188 $cReviewHolder2 = $xdoc ->createElement('Review'); | |
189 $cReview2 = $xdoc ->createElement('Content'); | |
190 $cReviewHolder3 = $xdoc ->createElement('Review'); | |
191 $cReview3 = $xdoc ->createElement('Content'); | |
192 | |
193 $txtNode = $xdoc ->createTextNode ($review1); | |
194 $cReview -> appendChild($txtNode); | |
195 | |
196 $txtNode2 = $xdoc ->createTextNode ($review2); | |
197 $cReview2 -> appendChild($txtNode2); | |
198 | |
199 $txtNode3 = $xdoc ->createTextNode ($review3); | |
200 $cReview3 -> appendChild($txtNode3); | |
201 | |
202 $cReviewHolder -> appendChild($cReview); | |
203 $cReviewHolder2 -> appendChild($cReview2); | |
204 $cReviewHolder3 -> appendChild($cReview3); | |
205 | |
206 $cReviews -> appendChild($cReviewHolder); | |
207 $cReviews -> appendChild($cReviewHolder2); | |
208 $cReviews -> appendChild($cReviewHolder3); | |
209 | |
210 $xdoc->documentElement->childNodes->item(1)->childNodes->item(1)->appendChild($cReviews); | |
211 $newXML = simplexml_import_dom($xdoc); | |
212 $output = $newXML->asXml(); | |
213 } | |
214 | |
215 switch($locale){ | |
216 case "us": | |
217 $loc = 0; | |
218 break; | |
219 case "uk": | |
220 $loc = 1; | |
221 break; | |
222 case "ca": | |
223 $loc = 2; | |
224 break; | |
225 case "de": | |
226 $loc = 3; | |
227 break; | |
228 case "fr": | |
229 $loc = 4; | |
230 break; | |
231 }; | |
232 | |
233 $errorCode = $xml->Error->Code; | |
234 //echo $errorCode; | |
235 | |
236 if($errorCode != "AccountLimitExceeded") | |
237 { | |
238 if($go == 1) | |
239 return $output; | |
240 else | |
241 { | |
242 $title = $xml->Items->Item->ItemAttributes->Title; | |
243 $author = $xml->Items->Item->ItemAttributes->Author; | |
244 $binding = $xml->Items->Item->ItemAttributes->Binding; | |
245 $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber; | |
246 if($dewey == "") | |
247 $dewey = "null"; | |
248 $imageURL = $xml->Items->Item->MediumImage->URL; | |
249 $salesRank = $xml->Items->Item->SalesRank; | |
250 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate; | |
251 $publisher = $xml->Items->Item->ItemAttributes->Publisher; | |
252 | |
253 $genreID = ""; | |
254 $genre = ""; | |
255 $genArr = array(); | |
256 | |
257 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){ | |
258 //sexy recursive function | |
259 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre); | |
260 | |
261 if($genre != "") | |
262 $genArr[strval($genreID)] = strval($genre); | |
263 //$genArr[$i] = array(strval($genreID) => strval($genre)); | |
264 | |
265 //echo $genre; | |
266 //echo $genreID; | |
267 | |
268 $genre = ""; | |
269 $genreID = ""; | |
270 } | |
271 | |
272 $g1 = "null"; | |
273 $g2 = "null"; | |
274 $g3 = "null"; | |
275 $loop = 1; | |
276 | |
277 foreach ($genArr as $key => $value) { | |
278 //echo "$key => $value"; | |
279 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table | |
280 //echo $queryG; | |
281 include "../../private/db.php"; | |
282 $resG = mysqli_query($link, $queryG); | |
283 mysqli_close($link); | |
284 | |
285 switch ($loop) { | |
286 case 1: | |
287 $g1 = $key; | |
288 break; | |
289 case 2: | |
290 $g2 = $key; | |
291 break; | |
292 case 3: | |
293 $g3 = $key; | |
294 break; | |
295 } | |
296 | |
297 $loop++; | |
298 } | |
299 | |
300 if($salesRank == "") | |
301 $salesRank = "null"; | |
302 | |
303 $title = strtr($title, '"', "'"); | |
304 include "../../private/db.php"; | |
305 $review1 = mysqli_real_escape_string($link,$review1); | |
306 $review2 = mysqli_real_escape_string($link,$review2); | |
307 $review3 = mysqli_real_escape_string($link,$review3); | |
308 | |
309 if($title != "") | |
310 { | |
311 $queryInsert = "CALL b_addNewBook(\"$searchparameterdata\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,\"$pubDate\",\"$publisher\",$g1,$g2,$g3,$loc)"; | |
312 //echo $queryInsert; | |
313 $queryInsertReviews = "CALL b_insertReviews(\"$searchparameterdata\",\"$review1\",\"$review2\",\"$review3\")"; | |
314 | |
315 $resG = mysqli_query($link, $queryInsert) or exit( mysqli_error( $link )); | |
316 if($review1 != "" && $review2 != "" && $review3 != "") | |
317 $resG = mysqli_query($link, $queryInsertReviews) or exit( mysqli_error( $link )); | |
318 | |
319 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | |
320 } | |
321 | |
322 echo $output; | |
323 } | |
324 } | |
325 else | |
326 { | |
327 //look up info from db | |
328 include "../../private/db.php"; | |
329 $query = "CALL b_getBookInfo('$searchparameterdata', $loc)"; | |
330 //echo $query; | |
331 $res = mysqli_query($link, $query) or exit( mysqli_error( $link )); | |
332 | |
333 $output = ""; | |
334 $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; | |
335 $output .= "<Details>"; | |
336 if ( mysqli_num_rows( $res ) > 0 ) | |
337 { | |
338 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); | |
339 | |
340 $output .= "<ASIN>" . $searchparameterdata . "</ASIN>"; | |
341 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>"; | |
342 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>"; | |
343 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>"; | |
344 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>"; | |
345 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>"; | |
346 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>"; | |
347 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>"; | |
348 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>"; | |
349 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>"; | |
350 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; | |
351 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; | |
352 $output .= "<ProductGroup>Book</ProductGroup>"; | |
353 $output .= "<Error>AccountLimitExceeded</Error>"; | |
354 | |
355 } | |
356 $output .= "</Details>"; | |
357 echo $output; | |
358 } | |
359 } | |
360 | |
361 function findGenre($browseNode, &$ID, &$gen) | |
362 { | |
363 if($browseNode->Name == "Subjects") | |
364 { | |
365 return true; | |
366 } | |
367 else | |
368 { | |
369 if($browseNode->Ancestors->BrowseNode) | |
370 { | |
371 if(findGenre($browseNode->Ancestors->BrowseNode, $ID, $gen) == true) | |
372 { | |
373 $gen = $browseNode->Name; | |
374 $ID = $browseNode->BrowseNodeId; | |
375 } | |
376 } | |
377 return false; | |
378 } | |
379 } | |
380 | |
381 if(!isset($ret)) | |
382 { | |
383 include "aws_signed_request.php"; | |
384 getAmazonDet('default',0,'us'); //will get overwritten | |
385 } | |
386 /*Caught oneSimpleXMLElement Object | |
387 ( | |
388 [OperationRequest] => SimpleXMLElement Object | |
389 ( | |
390 [RequestId] => d2eaacba-2411-44e7-b268-f23a20167330 | |
391 [Arguments] => SimpleXMLElement Object | |
392 ( | |
393 [Argument] => Array | |
394 ( | |
395 [0] => SimpleXMLElement Object | |
396 ( | |
397 [@attributes] => Array | |
398 ( | |
399 [Name] => AWSAccessKeyId | |
400 [Value] => AKIAJBXEHTNCU6LLFNIA | |
401 ) | |
402 | |
403 ) | |
404 | |
405 [1] => SimpleXMLElement Object | |
406 ( | |
407 [@attributes] => Array | |
408 ( | |
409 [Name] => AssociateTag | |
410 [Value] => bookwhack-21 | |
411 ) | |
412 | |
413 ) | |
414 | |
415 [2] => SimpleXMLElement Object | |
416 ( | |
417 [@attributes] => Array | |
418 ( | |
419 [Name] => ItemId | |
420 [Value] => B004Q3Q3Y4 | |
421 ) | |
422 | |
423 ) | |
424 | |
425 [3] => SimpleXMLElement Object | |
426 ( | |
427 [@attributes] => Array | |
428 ( | |
429 [Name] => ItemPage | |
430 [Value] => 1 | |
431 ) | |
432 | |
433 ) | |
434 | |
435 [4] => SimpleXMLElement Object | |
436 ( | |
437 [@attributes] => Array | |
438 ( | |
439 [Name] => Operation | |
440 [Value] => ItemLookup | |
441 ) | |
442 | |
443 ) | |
444 | |
445 [5] => SimpleXMLElement Object | |
446 ( | |
447 [@attributes] => Array | |
448 ( | |
449 [Name] => ResponseGroup | |
450 [Value] => ItemAttributes,Reviews,EditorialReview,OfferSummary,Offers,Images,AlternateVersions,SalesRank,BrowseNodes | |
451 ) | |
452 | |
453 ) | |
454 | |
455 [6] => SimpleXMLElement Object | |
456 ( | |
457 [@attributes] => Array | |
458 ( | |
459 [Name] => ReviewSort | |
460 [Value] => -HelpfulVotes | |
461 ) | |
462 | |
463 ) | |
464 | |
465 [7] => SimpleXMLElement Object | |
466 ( | |
467 [@attributes] => Array | |
468 ( | |
469 [Name] => Service | |
470 [Value] => AWSECommerceService | |
471 ) | |
472 | |
473 ) | |
474 | |
475 [8] => SimpleXMLElement Object | |
476 ( | |
477 [@attributes] => Array | |
478 ( | |
479 [Name] => Timestamp | |
480 [Value] => 2016-12-15T23:12:34Z | |
481 ) | |
482 | |
483 ) | |
484 | |
485 [9] => SimpleXMLElement Object | |
486 ( | |
487 [@attributes] => Array | |
488 ( | |
489 [Name] => Version | |
490 [Value] => 2011-08-01 | |
491 ) | |
492 | |
493 ) | |
494 | |
495 [10] => SimpleXMLElement Object | |
496 ( | |
497 [@attributes] => Array | |
498 ( | |
499 [Name] => Signature | |
500 [Value] => SUXfFZHQ74Joc+WDLx87uzemTdtHijNohykqafJXYKQ= | |
501 ) | |
502 | |
503 ) | |
504 | |
505 ) | |
506 | |
507 ) | |
508 | |
509 [RequestProcessingTime] => 0.3518217620000000 | |
510 ) | |
511 | |
512 [Items] => SimpleXMLElement Object | |
513 ( | |
514 [Request] => SimpleXMLElement Object | |
515 ( | |
516 [IsValid] => True | |
517 [ItemLookupRequest] => SimpleXMLElement Object | |
518 ( | |
519 [IdType] => ASIN | |
520 [ItemId] => B004Q3Q3Y4 | |
521 [ResponseGroup] => Array | |
522 ( | |
523 [0] => ItemAttributes | |
524 [1] => Reviews | |
525 [2] => EditorialReview | |
526 [3] => OfferSummary | |
527 [4] => Offers | |
528 [5] => Images | |
529 [6] => AlternateVersions | |
530 [7] => SalesRank | |
531 [8] => BrowseNodes | |
532 ) | |
533 | |
534 [VariationPage] => All | |
535 ) | |
536 | |
537 [Errors] => SimpleXMLElement Object | |
538 ( | |
539 [Error] => SimpleXMLElement Object | |
540 ( | |
541 [Code] => AWS.InvalidParameterValue | |
542 [Message] => B004Q3Q3Y4 is not a valid value for ItemId. Please change this value and retry your request. | |
543 ) | |
544 | |
545 ) | |
546 | |
547 ) | |
548 | |
549 ) | |
550 | |
551 ) | |
552 */ | |
553 ?> |