Mercurial > hg > ywww
annotate xml/getAmazonInfo.php @ 25:828895488948
more db column protection
author | Robert Boland <robert@markup.co.uk> |
---|---|
date | Tue, 01 Jan 2019 07:30:05 -0500 |
parents | d606320ec331 |
children | 4124f103b46b |
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(); |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
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 } |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
147 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
|
148 if ($code=='RequestThrottled') { |
23
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
149 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
|
150 // principled solution in place |
7
1dfe64e365a0
a bit more debugging/bomb-proofing
Robert Boland <robert@markup.co.uk>
parents:
5
diff
changeset
|
151 } |
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
7
diff
changeset
|
152 else { |
18 | 153 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
|
154 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
|
155 if ($code=="") { |
11
c9ebf871114b
fix wrong var in debug output
Henry S. Thompson <ht@markup.co.uk>
parents:
10
diff
changeset
|
156 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
|
157 } |
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
7
diff
changeset
|
158 } |
5 | 159 } |
160 else { | |
161 $xml = new SimpleXMLElement($output); | |
162 set_error_handler(function () { | |
163 global $output; | |
164 file_put_contents('/var/ywww/debug/phpDebug', | |
165 "Caught one?: ".$searchparameterdata, | |
166 FILE_APPEND); | |
167 file_put_contents('/var/ywww/debug/phpDebug', | |
168 print_r($output, TRUE)."\n", | |
169 FILE_APPEND); | |
4 | 170 } ); |
5 | 171 $review = $xml->Items->Item->CustomerReviews->IFrameURL; |
172 // The above is failing repeatedly -- | |
173 //PHP Notice: Trying to get property of non-object in | |
174 // /var/ywww/xml/getAmazonInfo.php on line [109] | |
175 // See the dumped structure at the end of this file for the | |
176 // cause | |
177 restore_error_handler(); | |
178 //echo $review; | |
179 } | |
4 | 180 if ($review != "") |
181 { | |
182 $text = @file_get_contents($review . "&truncate=300"); | |
183 $removeTop1 = preg_replace('~<div class="crIFrameHeaderLeftColumn">(.*?)<div class="crIFrameHeaderHistogram">~si', '', $text); | |
184 $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
|
185 if (preg_match('~<body[^>]*>(.*?)</body>~si', $removeTop2, $body)){ $getBody = trim($body[1]); } else { $getBody = $removeTop2;} |
4 | 186 $removeDiv = preg_replace('~<div[^>]*>(.*?)</div>~si', '', $getBody); |
187 $removeCloseDivs = preg_replace('/<\/div>/','', $removeDiv); | |
188 $setBoundary = str_replace('<!-- BOUNDARY -->','BOTTOM-TOP', $removeCloseDivs); | |
189 //replace <!-- BOUNDARY --> with BOTTOM-TOP | |
190 $remove1 = '~<table cellpadding="0"(.*?)%">~s'; | |
191 $setBoundary = preg_replace($remove1,'', $setBoundary); | |
192 $remove2 = '~</td><td bg(.*?)</table>~s'; | |
193 $setBoundary = preg_replace($remove2,'', $setBoundary); | |
194 $remove3 = '~<a name=(.*?)</a>~s'; | |
195 $setBoundary = preg_replace($remove3,'', $setBoundary); | |
196 $setBoundary2 = str_replace('<br />','', $setBoundary); | |
197 //remove all extra crap; | |
198 $setBoundary3 = str_replace('</td>','BOTTOM', $setBoundary2); | |
199 //replace </td> with BOTTOM | |
200 | |
201 if (preg_match_all('~TOP(.*?)BOTTOM~s', $setBoundary3, $reviews)) | |
202 { | |
203 $reviewContents = $reviews[1]; | |
204 //print_r($reviewContents); | |
205 $review1 = trim($reviewContents[0]); | |
206 $review1 = str_replace("\n", "", $review1); | |
207 $review1 = str_replace("\r", "", $review1); | |
208 if (isset($reviewContents[1])) { | |
209 $review2 = trim($reviewContents[1]); | |
210 $review2 = str_replace("\n", "", $review2); | |
211 $review2 = str_replace("\r", "", $review2); | |
212 } | |
213 else { | |
214 $review2 = ""; | |
215 } | |
216 if (isset($reviewContents[2])) { | |
217 $review3 = trim($reviewContents[2]); | |
218 $review3 = str_replace("\n", "", $review3); | |
219 $review3 = str_replace("\r", "", $review3); | |
220 } | |
221 else { | |
222 $review3 = ""; | |
223 } | |
224 } | |
225 else | |
226 { | |
227 $review1 = ""; | |
228 $review2 = ""; | |
229 $review3 = ""; | |
230 //echo "EPIC FAIL"; | |
231 } | |
232 | |
233 unset($xml->Items->Item->CustomerReviews); | |
234 $xdoc = new DomDocument; | |
235 $xdoc->loadXML($xml->asXML()); | |
236 | |
237 $cReviews = $xdoc ->createElement('CustomerReviews'); | |
238 $cReviewHolder = $xdoc ->createElement('Review'); | |
239 $cReview = $xdoc ->createElement('Content'); | |
240 $cReviewHolder2 = $xdoc ->createElement('Review'); | |
241 $cReview2 = $xdoc ->createElement('Content'); | |
242 $cReviewHolder3 = $xdoc ->createElement('Review'); | |
243 $cReview3 = $xdoc ->createElement('Content'); | |
244 | |
245 $txtNode = $xdoc ->createTextNode ($review1); | |
246 $cReview -> appendChild($txtNode); | |
247 | |
248 $txtNode2 = $xdoc ->createTextNode ($review2); | |
249 $cReview2 -> appendChild($txtNode2); | |
250 | |
251 $txtNode3 = $xdoc ->createTextNode ($review3); | |
252 $cReview3 -> appendChild($txtNode3); | |
253 | |
254 $cReviewHolder -> appendChild($cReview); | |
255 $cReviewHolder2 -> appendChild($cReview2); | |
256 $cReviewHolder3 -> appendChild($cReview3); | |
257 | |
258 $cReviews -> appendChild($cReviewHolder); | |
259 $cReviews -> appendChild($cReviewHolder2); | |
260 $cReviews -> appendChild($cReviewHolder3); | |
261 | |
262 $xdoc->documentElement->childNodes->item(1)->childNodes->item(1)->appendChild($cReviews); | |
263 $newXML = simplexml_import_dom($xdoc); | |
264 $output = $newXML->asXml(); | |
265 } | |
266 | |
267 switch($locale){ | |
268 case "us": | |
269 $loc = 0; | |
270 break; | |
271 case "uk": | |
272 $loc = 1; | |
273 break; | |
274 case "ca": | |
275 $loc = 2; | |
276 break; | |
277 case "de": | |
278 $loc = 3; | |
279 break; | |
280 case "fr": | |
281 $loc = 4; | |
282 break; | |
283 }; | |
284 | |
285 $errorCode = $xml->Error->Code; | |
286 //echo $errorCode; | |
287 | |
288 if($errorCode != "AccountLimitExceeded") | |
289 { | |
5 | 290 if($go == 1) { |
19 | 291 $item = $xml->Items->Item[0]; |
292 if ($item && $item->ItemAttributes && $item->ItemAttributes->Title) { | |
293 $title = $item->ItemAttributes->Title; } | |
294 else { $title = "[no title]"; }; | |
5 | 295 file_put_contents('/var/ywww/debug/phpDebug',"win: ". |
19 | 296 $title."\n", |
5 | 297 FILE_APPEND); |
4 | 298 return $output; |
5 | 299 } |
19 | 300 else |
301 { | |
302 if ($xml->Items->Item) { | |
303 $title = $xml->Items->Item[0]->ItemAttributes->Title; | |
304 $author = $xml->Items->Item[0]->ItemAttributes->Author; | |
305 $binding = $xml->Items->Item[0]->ItemAttributes->Binding; | |
306 $dewey = $xml->Items->Item[0]->ItemAttributes->DeweyDecimalNumber; | |
307 if($dewey == "") | |
308 $dewey = "null"; | |
309 $imageURL = $xml->Items->Item[0]->MediumImage->URL; | |
310 $salesRank = $xml->Items->Item[0]->SalesRank; | |
311 $pubDate = $xml->Items->Item[0]->ItemAttributes->PublicationDate; | |
25
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
312 if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";} |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
313 if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";} |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
314 if (strlen($pubDate)==0) { |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
315 $pubDate="null"; |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
316 } |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
317 else { |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
318 $pubDate="\"$pubDate\""; |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
319 } |
19 | 320 $publisher = $xml->Items->Item[0]->ItemAttributes->Publisher; |
25
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
321 if ($publisher and strlen($publisher)>30) { |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
322 $publisher=substr($publisher,0,30); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
323 } |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
324 if ($author and strlen($author)>30) { |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
325 $author=substr($author,0,30); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
326 } |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
327 if ($title and strlen($title)>100) { |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
328 $title=substr($title,0,100); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
329 } |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
330 $publisher=mysqli_real_escape_string($link,$publisher); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
331 $author=mysqli_real_escape_string($link,$author); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
332 $title=mysqli_real_escape_string($link,$title); |
19 | 333 } |
334 else { | |
335 $title = $salesRank = ""; | |
336 $dewey = "null"; | |
337 } | |
4 | 338 |
19 | 339 $genreID = ""; |
340 $genre = ""; | |
341 $genArr = array(); | |
342 | |
343 if ($xml->Items->Item[0] && $xml->Items->Item[0]->BrowseNodes) { | |
344 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){ | |
345 //sexy recursive function | |
346 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre); | |
4 | 347 |
19 | 348 if($genre != "") |
349 $genArr[strval($genreID)] = strval($genre); | |
350 //$genArr[$i] = array(strval($genreID) => strval($genre)); | |
4 | 351 |
19 | 352 //echo $genre; |
353 //echo $genreID; | |
4 | 354 |
19 | 355 $genre = ""; |
356 $genreID = ""; | |
357 } | |
358 } | |
4 | 359 |
19 | 360 $g1 = "null"; |
361 $g2 = "null"; | |
362 $g3 = "null"; | |
363 $loop = 1; | |
4 | 364 |
19 | 365 foreach ($genArr as $key => $value) { |
366 //echo "$key => $value"; | |
23
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
367 if ($key>2047) { |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
368 //HST added |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
369 break; |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
370 } |
19 | 371 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table |
372 //echo $queryG; | |
373 include "../../private/db.php"; | |
374 $resG = mysqli_query($link, $queryG); | |
375 mysqli_close($link); | |
4 | 376 |
19 | 377 switch ($loop) { |
378 case 1: | |
379 $g1 = $key; | |
380 break; | |
381 case 2: | |
382 $g2 = $key; | |
383 break; | |
384 case 3: | |
385 $g3 = $key; | |
386 break; | |
387 } | |
4 | 388 |
19 | 389 $loop++; |
390 } | |
4 | 391 |
19 | 392 if($salesRank == "") |
393 $salesRank = "null"; | |
4 | 394 |
19 | 395 $title = strtr($title, '"', "'"); |
396 include "../../private/db.php"; | |
397 $review1 = mysqli_real_escape_string($link,$review1); | |
25
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
398 if (strlen($review1)>500) { $review1=substr($review1,0,500);} |
19 | 399 $review2 = mysqli_real_escape_string($link,$review2); |
25
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
400 if (strlen($review2)>500) { $review2=substr($review2,0,500);} |
19 | 401 $review3 = mysqli_real_escape_string($link,$review3); |
25
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
402 if (strlen($review3)>500) { $review3=substr($review3,0,500);} |
4 | 403 |
19 | 404 if($title != "") |
405 { | |
406 $queryInsert = "CALL b_addNewBook(\"$searchparameterdata\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,\"$pubDate\",\"$publisher\",$g1,$g2,$g3,$loc)"; | |
407 //echo $queryInsert; | |
25
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
408 |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
409 $res = mysqli_query($link, $queryInsert); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
410 if (!$res) { |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
411 $err=mysqli_error( $link ); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
412 mysqli_close($link); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
413 file_put_contents('/var/ywww/debug/phpDebug', |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
414 "anb failed: $err, $pubDate, $g2, $publisher, $title\n", |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
415 FILE_APPEND); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
416 exit($err); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
417 } |
19 | 418 $queryInsertReviews = "CALL b_insertReviews(\"$searchparameterdata\",\"$review1\",\"$review2\",\"$review3\")"; |
25
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
419 if($review1 != "" && $review2 != "" && $review3 != "") { |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
420 $res = mysqli_query($link, $queryInsertReviews); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
421 if (!$res) { |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
422 $err=mysqli_error( $link ); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
423 mysqli_close($link); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
424 file_put_contents('/var/ywww/debug/phpDebug', |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
425 "anr failed: $err, $pubDate, $g2, $publisher, $title\n", |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
426 FILE_APPEND); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
427 exit($err); |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
428 } |
828895488948
more db column protection
Robert Boland <robert@markup.co.uk>
parents:
23
diff
changeset
|
429 } |
19 | 430 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
431 } | |
4 | 432 |
19 | 433 echo $output; |
434 } | |
4 | 435 } |
436 else | |
437 { | |
438 //look up info from db | |
439 include "../../private/db.php"; | |
440 $query = "CALL b_getBookInfo('$searchparameterdata', $loc)"; | |
441 //echo $query; | |
442 $res = mysqli_query($link, $query) or exit( mysqli_error( $link )); | |
443 | |
444 $output = ""; | |
445 $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; | |
446 $output .= "<Details>"; | |
447 if ( mysqli_num_rows( $res ) > 0 ) | |
448 { | |
449 $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
|
450 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
4 | 451 $output .= "<ASIN>" . $searchparameterdata . "</ASIN>"; |
452 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>"; | |
453 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>"; | |
454 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>"; | |
455 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>"; | |
456 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>"; | |
457 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>"; | |
458 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>"; | |
459 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>"; | |
460 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>"; | |
461 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; | |
462 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; | |
463 $output .= "<ProductGroup>Book</ProductGroup>"; | |
464 $output .= "<Error>AccountLimitExceeded</Error>"; | |
465 | |
23
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
466 } |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
467 else { |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
468 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
|
469 } |
4 | 470 $output .= "</Details>"; |
471 echo $output; | |
472 } | |
473 } | |
474 | |
475 function findGenre($browseNode, &$ID, &$gen) | |
476 { | |
477 if($browseNode->Name == "Subjects") | |
478 { | |
479 return true; | |
480 } | |
481 else | |
482 { | |
483 if($browseNode->Ancestors->BrowseNode) | |
484 { | |
485 if(findGenre($browseNode->Ancestors->BrowseNode, $ID, $gen) == true) | |
486 { | |
487 $gen = $browseNode->Name; | |
488 $ID = $browseNode->BrowseNodeId; | |
489 } | |
490 } | |
491 return false; | |
492 } | |
493 } | |
494 | |
495 if(!isset($ret)) | |
496 { | |
497 include "aws_signed_request.php"; | |
498 getAmazonDet('default',0,'us'); //will get overwritten | |
499 } | |
500 /*Caught oneSimpleXMLElement Object | |
501 ( | |
502 [OperationRequest] => SimpleXMLElement Object | |
503 ( | |
504 [RequestId] => d2eaacba-2411-44e7-b268-f23a20167330 | |
505 [Arguments] => SimpleXMLElement Object | |
506 ( | |
507 [Argument] => Array | |
508 ( | |
509 [0] => SimpleXMLElement Object | |
510 ( | |
511 [@attributes] => Array | |
512 ( | |
513 [Name] => AWSAccessKeyId | |
17
ae1459564f66
Replace old associate keys with new IAMs user keys
Robert Boland <robert@markup.co.uk>
parents:
11
diff
changeset
|
514 [Value] => AKIAIHTNWC7L6LOUY4LQ |
4 | 515 ) |
516 | |
517 ) | |
518 | |
519 [1] => SimpleXMLElement Object | |
520 ( | |
521 [@attributes] => Array | |
522 ( | |
523 [Name] => AssociateTag | |
524 [Value] => bookwhack-21 | |
525 ) | |
526 | |
527 ) | |
528 | |
529 [2] => SimpleXMLElement Object | |
530 ( | |
531 [@attributes] => Array | |
532 ( | |
533 [Name] => ItemId | |
534 [Value] => B004Q3Q3Y4 | |
535 ) | |
536 | |
537 ) | |
538 | |
539 [3] => SimpleXMLElement Object | |
540 ( | |
541 [@attributes] => Array | |
542 ( | |
543 [Name] => ItemPage | |
544 [Value] => 1 | |
545 ) | |
546 | |
547 ) | |
548 | |
549 [4] => SimpleXMLElement Object | |
550 ( | |
551 [@attributes] => Array | |
552 ( | |
553 [Name] => Operation | |
554 [Value] => ItemLookup | |
555 ) | |
556 | |
557 ) | |
558 | |
559 [5] => SimpleXMLElement Object | |
560 ( | |
561 [@attributes] => Array | |
562 ( | |
563 [Name] => ResponseGroup | |
564 [Value] => ItemAttributes,Reviews,EditorialReview,OfferSummary,Offers,Images,AlternateVersions,SalesRank,BrowseNodes | |
565 ) | |
566 | |
567 ) | |
568 | |
569 [6] => SimpleXMLElement Object | |
570 ( | |
571 [@attributes] => Array | |
572 ( | |
573 [Name] => ReviewSort | |
574 [Value] => -HelpfulVotes | |
575 ) | |
576 | |
577 ) | |
578 | |
579 [7] => SimpleXMLElement Object | |
580 ( | |
581 [@attributes] => Array | |
582 ( | |
583 [Name] => Service | |
584 [Value] => AWSECommerceService | |
585 ) | |
586 | |
587 ) | |
588 | |
589 [8] => SimpleXMLElement Object | |
590 ( | |
591 [@attributes] => Array | |
592 ( | |
593 [Name] => Timestamp | |
594 [Value] => 2016-12-15T23:12:34Z | |
595 ) | |
596 | |
597 ) | |
598 | |
599 [9] => SimpleXMLElement Object | |
600 ( | |
601 [@attributes] => Array | |
602 ( | |
603 [Name] => Version | |
604 [Value] => 2011-08-01 | |
605 ) | |
606 | |
607 ) | |
608 | |
609 [10] => SimpleXMLElement Object | |
610 ( | |
611 [@attributes] => Array | |
612 ( | |
613 [Name] => Signature | |
614 [Value] => SUXfFZHQ74Joc+WDLx87uzemTdtHijNohykqafJXYKQ= | |
615 ) | |
616 | |
617 ) | |
618 | |
619 ) | |
620 | |
621 ) | |
622 | |
623 [RequestProcessingTime] => 0.3518217620000000 | |
624 ) | |
625 | |
626 [Items] => SimpleXMLElement Object | |
627 ( | |
628 [Request] => SimpleXMLElement Object | |
629 ( | |
630 [IsValid] => True | |
631 [ItemLookupRequest] => SimpleXMLElement Object | |
632 ( | |
633 [IdType] => ASIN | |
634 [ItemId] => B004Q3Q3Y4 | |
635 [ResponseGroup] => Array | |
636 ( | |
637 [0] => ItemAttributes | |
638 [1] => Reviews | |
639 [2] => EditorialReview | |
640 [3] => OfferSummary | |
641 [4] => Offers | |
642 [5] => Images | |
643 [6] => AlternateVersions | |
644 [7] => SalesRank | |
645 [8] => BrowseNodes | |
646 ) | |
647 | |
648 [VariationPage] => All | |
649 ) | |
650 | |
651 [Errors] => SimpleXMLElement Object | |
652 ( | |
653 [Error] => SimpleXMLElement Object | |
654 ( | |
655 [Code] => AWS.InvalidParameterValue | |
656 [Message] => B004Q3Q3Y4 is not a valid value for ItemId. Please change this value and retry your request. | |
657 ) | |
658 | |
659 ) | |
660 | |
661 ) | |
662 | |
663 ) | |
664 | |
665 ) | |
666 */ | |
21
46382face560
use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents:
19
diff
changeset
|
667 /*$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 | 668 ?> |