Mercurial > hg > ywww
annotate xml/doAmazonRequest.php @ 59:e4c78b3eace7
stop bothering Amazon.co.uk at all
author | Charlie Root |
---|---|
date | Sun, 09 Jun 2019 06:49:35 -0400 |
parents | 72708ec9c0e6 |
children |
rev | line source |
---|---|
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
1 <?php |
49
a67bf725e87b
put both paths in include_path and depend on that
Charlie Root
parents:
44
diff
changeset
|
2 include_once "dlog.php"; |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
3 |
49
a67bf725e87b
put both paths in include_path and depend on that
Charlie Root
parents:
44
diff
changeset
|
4 include_once "web.php"; |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
5 include "aws_signed_request.php"; |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
6 |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
7 function doAmazonRequest($ext, $parameters, $try, $dbl='x', $wantXML=True) |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
8 { |
59 | 9 /* Uncomment next line to disable all requests from UK site */ |
10 if ($ext=="co.uk") { | |
11 throw new Exception("ThrottledRepeatedly"); | |
12 } | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
13 global $public_key, $private_key; |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
14 $requestURI = $_SERVER['REQUEST_URI']; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
15 $requestIP = $_SERVER['REMOTE_ADDR']; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
16 $file_data=$ext; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
17 ksort($parameters); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
18 foreach ($parameters as $i=>$d) { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
19 $file_data.='&'.$i.'='.$d; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
20 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
21 $gotit=0; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
22 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
23 //dl("search: $ext $public_key $private_key\n$url\n".print_r($parameters,TRUE)."\n"); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
24 for ($i=1; $i<=$try; $i++) { |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
25 dl("dar: $requestIP $i\n"); |
37 | 26 $crl = curl_init(); |
27 $timeout = 5; | |
28 curl_setopt ($crl, CURLOPT_URL,$url); | |
29 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); | |
30 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | |
31 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
32 $semaphore = new SyncSemaphore("Amazon".$ext); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
33 $gotit = $semaphore->lock(5000); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
34 if (!$gotit) { |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
35 dl("No lock: $requestIP $dbl\n"); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
36 for ($j=1; $j<=$try+4; $j++) { |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
37 dl("dar: $requestIP $i $j\n"); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
38 $gotit=$semaphore->lock(1000); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
39 if ($gotit) { |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
40 dl("Got it: $i $j $requestIP $dbl"."a\n"); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
41 break; |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
42 } |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
43 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
44 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
45 else { |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
46 dl("Got it: $i ! $requestIP $dbl"."a\n"); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
47 } |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
48 if (!$gotit) { |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
49 usleep(1000000); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
50 continue; |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
51 } |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
52 usleep(1000000); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
53 $output = curl_exec($crl); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
54 dl("nai2: $gotit ".strlen($output)."\n"); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
55 curl_close($crl); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
56 $mm=array(); |
55 | 57 try { |
58 $xml = new SimpleXMLElement($output); | |
59 } | |
60 catch (Exception $e) { | |
61 dl("dar: bad output:\n$output\n"); | |
62 throw $e; | |
63 } | |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
64 if (preg_match("/<Error>/",$output,$mm)) { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
65 $resName=$xml->getName(); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
66 $code=$xml->Error->Code; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
67 if ($code) { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
68 $message=$xml->Error->Message; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
69 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
70 else { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
71 $code=$xml->Items->Request->Errors->Error->Code; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
72 $message=$xml->Items->Request->Errors->Error->Message; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
73 } |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
74 dl("Losing $i: $dbl ". |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
75 $mm[0].", $resName, $code, $requestIP, $requestURI\n"); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
76 if ($code!='RequestThrottled') { |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
77 dl("message: $message\n"); |
37 | 78 if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) { |
79 // Check for common problem and try to fix... | |
80 $spd=$parameters['ItemId']; | |
81 if (strpos($spd,'/')>0) { | |
82 $isbnMaybe=substr($spd,0,strpos($spd,'/')); | |
83 $parameters['ItemId']=$isbnMaybe; | |
84 if (isset($parameters['Keywords'])) { | |
85 $parameters['Keywords']=urlencode($isbnMaybe); | |
86 } | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
87 dl("retrying with $isbnMaybe\n"); |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
88 if ($gotit) { |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
89 $semaphore->unlock(); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
90 } |
38 | 91 return doAmazonRequest($ext,$parameters,$try,$dbl); |
37 | 92 } |
93 } | |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
94 if ($code=="") { |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
95 dl("error elt:\n$output\n"); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
96 } |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
97 if ($gotit) { |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
98 $semaphore->unlock(); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
99 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
100 throw new Exception($code); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
101 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
102 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
103 else { |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
104 if ($gotit) { |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
105 $cnt=0; |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
106 $semaphore->unlock($cnt); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
107 dl("unlocked: $i $cnt\n"); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
108 } |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
109 if ($wantXML) { |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
110 return $xml; |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
111 } |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
112 else { |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
113 //dl("returning ".strlen($output)."\n"); |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
114 return $output; |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
115 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
116 } |
38 | 117 usleep(100000*$i); // Try to reduce throttling until we get a |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
118 // principled solution in place |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
119 if ($gotit) { |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
120 $semaphore->unlock(); |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
121 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
122 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
123 throw new Exception("ThrottledRepeatedly"); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
124 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
125 ?> |