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