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