Mercurial > hg > ywww
comparison xml/doAmazonRequest.inc @ 38:c24ae74bf6d5
pass char for log lines in to doAmazonRequest
author | Charlie Root |
---|---|
date | Fri, 04 Jan 2019 18:02:20 -0500 |
parents | 633402a4995d |
children | dbc006408d2b |
comparison
equal
deleted
inserted
replaced
37:633402a4995d | 38:c24ae74bf6d5 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 function doAmazonRequest($ext, $parameters, $try) | 3 function doAmazonRequest($ext, $parameters, $try, $dbl='x') |
4 { | 4 { |
5 $public_key ="AKIAIHTNWC7L6LOUY4LQ"; | 5 $public_key ="AKIAIHTNWC7L6LOUY4LQ"; |
6 $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde"; | 6 $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde"; |
7 $requestURI = $_SERVER['REQUEST_URI']; | 7 $requestURI = $_SERVER['REQUEST_URI']; |
8 $requestIP = $_SERVER['REMOTE_ADDR']; | 8 $requestIP = $_SERVER['REMOTE_ADDR']; |
24 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | 24 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); |
25 $semaphore = new SyncSemaphore("Amazon"); | 25 $semaphore = new SyncSemaphore("Amazon"); |
26 $gotit = $semaphore->lock(1000); | 26 $gotit = $semaphore->lock(1000); |
27 if (!$gotit) { | 27 if (!$gotit) { |
28 file_put_contents('/var/ywww/debug/phpDebug', | 28 file_put_contents('/var/ywww/debug/phpDebug', |
29 "Got it: ".(int)$gotit." $requestIP 1\n",FILE_APPEND); | 29 "Got it: ".(int)$gotit." $requestIP $dbl\n",FILE_APPEND); |
30 $gotit=$semaphore->lock(1000); | 30 $gotit=$semaphore->lock(1000); |
31 file_put_contents('/var/ywww/debug/phpDebug', | 31 file_put_contents('/var/ywww/debug/phpDebug', |
32 "Got it: ".(int)$gotit." $requestIP 1a\n",FILE_APPEND); | 32 "Got it: ".(int)$gotit." $requestIP $dbl"."a\n",FILE_APPEND); |
33 } | 33 } |
34 $output = curl_exec($crl); | 34 $output = curl_exec($crl); |
35 //file_put_contents('/var/ywww/debug/phpDebug', | 35 //file_put_contents('/var/ywww/debug/phpDebug', |
36 // "nai2: ".strlen($output)."\n",FILE_APPEND); | 36 // "nai2: ".strlen($output)."\n",FILE_APPEND); |
37 | 37 |
40 if ($gotit) { | 40 if ($gotit) { |
41 $semaphore->unlock(); | 41 $semaphore->unlock(); |
42 } | 42 } |
43 else { | 43 else { |
44 file_put_contents('/var/ywww/debug/phpDebug', | 44 file_put_contents('/var/ywww/debug/phpDebug', |
45 "W/o lock for $requestIP 1b\n",FILE_APPEND); | 45 "W/o lock for $requestIP $dbl"."b\n",FILE_APPEND); |
46 } | 46 } |
47 $mm=array(); | 47 $mm=array(); |
48 $xml = new SimpleXMLElement($output); | 48 $xml = new SimpleXMLElement($output); |
49 if (preg_match("/<Error>/",$output,$mm)) { | 49 if (preg_match("/<Error>/",$output,$mm)) { |
50 $resName=$xml->getName(); | 50 $resName=$xml->getName(); |
54 } | 54 } |
55 else { | 55 else { |
56 $code=$xml->Items->Request->Errors->Error->Code; | 56 $code=$xml->Items->Request->Errors->Error->Code; |
57 $message=$xml->Items->Request->Errors->Error->Message; | 57 $message=$xml->Items->Request->Errors->Error->Message; |
58 } | 58 } |
59 file_put_contents('/var/ywww/debug/phpDebug',"Losing $i: ".$mm[0].", $resName, $code, $requestIP, $requestURI\n",FILE_APPEND); | 59 file_put_contents('/var/ywww/debug/phpDebug',"Losing $i: $dbl ".$mm[0].", $resName, $code, $requestIP, $requestURI\n",FILE_APPEND); |
60 if ($code!='RequestThrottled') { | 60 if ($code!='RequestThrottled') { |
61 file_put_contents('/var/ywww/debug/phpDebug',"message: $message\n",FILE_APPEND); | 61 file_put_contents('/var/ywww/debug/phpDebug',"message: $message\n",FILE_APPEND); |
62 if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) { | 62 if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) { |
63 // Check for common problem and try to fix... | 63 // Check for common problem and try to fix... |
64 $spd=$parameters['ItemId']; | 64 $spd=$parameters['ItemId']; |
67 $parameters['ItemId']=$isbnMaybe; | 67 $parameters['ItemId']=$isbnMaybe; |
68 if (isset($parameters['Keywords'])) { | 68 if (isset($parameters['Keywords'])) { |
69 $parameters['Keywords']=urlencode($isbnMaybe); | 69 $parameters['Keywords']=urlencode($isbnMaybe); |
70 } | 70 } |
71 file_put_contents('/var/ywww/debug/phpDebug',"retrying with $isbnMaybe\n",FILE_APPEND); | 71 file_put_contents('/var/ywww/debug/phpDebug',"retrying with $isbnMaybe\n",FILE_APPEND); |
72 return doAmazonRequest($ext,$parameters,$try); | 72 return doAmazonRequest($ext,$parameters,$try,$dbl); |
73 } | 73 } |
74 } | 74 } |
75 if ($code=="") { | 75 if ($code=="") { |
76 file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$output\n",FILE_APPEND); | 76 file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$output\n",FILE_APPEND); |
77 } | 77 } |
79 } | 79 } |
80 } | 80 } |
81 else { | 81 else { |
82 return $xml; | 82 return $xml; |
83 } | 83 } |
84 usleep(200000); // Try to reduce throttling until we get a | 84 usleep(100000*$i); // Try to reduce throttling until we get a |
85 // principled solution in place | 85 // principled solution in place |
86 } | 86 } |
87 throw new Exception("ThrottledRepeatedly"); | 87 throw new Exception("ThrottledRepeatedly"); |
88 } | 88 } |
89 ?> | 89 ?> |