Mercurial > hg > ywww
annotate xml/doAmazonRequest.php @ 54:a04fc91bbd95
avoid warning on boolean
author | Charlie Root |
---|---|
date | Fri, 07 Jun 2019 16:35:16 -0400 |
parents | dd93cb4b77ad |
children | 72708ec9c0e6 |
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 { |
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
|
9 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
|
10 $requestURI = $_SERVER['REQUEST_URI']; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
11 $requestIP = $_SERVER['REMOTE_ADDR']; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
12 $file_data=$ext; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
13 ksort($parameters); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
14 foreach ($parameters as $i=>$d) { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
15 $file_data.='&'.$i.'='.$d; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
16 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
17 $gotit=0; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
18 $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
|
19 //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
|
20 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
|
21 dl("dar: $requestIP $i\n"); |
37 | 22 $crl = curl_init(); |
23 $timeout = 5; | |
24 curl_setopt ($crl, CURLOPT_URL,$url); | |
25 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); | |
26 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | |
27 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
|
28 $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
|
29 $gotit = $semaphore->lock(5000); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 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
|
34 $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
|
35 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
|
36 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
|
37 break; |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
38 } |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
39 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
40 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
41 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
|
42 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
|
43 } |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
44 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
|
45 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
|
46 continue; |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
47 } |
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
48 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
|
49 $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
|
50 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
|
51 curl_close($crl); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
52 $mm=array(); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
53 $xml = new SimpleXMLElement($output); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
54 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
|
55 $resName=$xml->getName(); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
56 $code=$xml->Error->Code; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
57 if ($code) { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
58 $message=$xml->Error->Message; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
59 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
60 else { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
61 $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
|
62 $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
|
63 } |
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
|
64 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
|
65 $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
|
66 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
|
67 dl("message: $message\n"); |
37 | 68 if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) { |
69 // Check for common problem and try to fix... | |
70 $spd=$parameters['ItemId']; | |
71 if (strpos($spd,'/')>0) { | |
72 $isbnMaybe=substr($spd,0,strpos($spd,'/')); | |
73 $parameters['ItemId']=$isbnMaybe; | |
74 if (isset($parameters['Keywords'])) { | |
75 $parameters['Keywords']=urlencode($isbnMaybe); | |
76 } | |
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("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
|
78 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
|
79 $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
|
80 } |
38 | 81 return doAmazonRequest($ext,$parameters,$try,$dbl); |
37 | 82 } |
83 } | |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
84 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
|
85 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
|
86 } |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
87 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
|
88 $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
|
89 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
90 throw new Exception($code); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
91 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
92 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
93 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
|
94 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
|
95 $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
|
96 $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
|
97 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
|
98 } |
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
|
99 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
|
100 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
|
101 } |
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
|
102 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
|
103 //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
|
104 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
|
105 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
106 } |
38 | 107 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
|
108 // 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
|
109 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
|
110 $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
|
111 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
112 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
113 throw new Exception("ThrottledRepeatedly"); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
114 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
115 ?> |