Mercurial > hg > ywww
annotate xml/doAmazonRequest.php @ 55:72708ec9c0e6
debug bad XML error
author | Charlie Root |
---|---|
date | Fri, 07 Jun 2019 16:35:35 -0400 |
parents | dd93cb4b77ad |
children | e4c78b3eace7 |
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(); |
55 | 53 try { |
54 $xml = new SimpleXMLElement($output); | |
55 } | |
56 catch (Exception $e) { | |
57 dl("dar: bad output:\n$output\n"); | |
58 throw $e; | |
59 } | |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
60 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
|
61 $resName=$xml->getName(); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
62 $code=$xml->Error->Code; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
63 if ($code) { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
64 $message=$xml->Error->Message; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
65 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
66 else { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
67 $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
|
68 $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
|
69 } |
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
|
70 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
|
71 $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
|
72 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
|
73 dl("message: $message\n"); |
37 | 74 if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) { |
75 // Check for common problem and try to fix... | |
76 $spd=$parameters['ItemId']; | |
77 if (strpos($spd,'/')>0) { | |
78 $isbnMaybe=substr($spd,0,strpos($spd,'/')); | |
79 $parameters['ItemId']=$isbnMaybe; | |
80 if (isset($parameters['Keywords'])) { | |
81 $parameters['Keywords']=urlencode($isbnMaybe); | |
82 } | |
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
|
83 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
|
84 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
|
85 $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
|
86 } |
38 | 87 return doAmazonRequest($ext,$parameters,$try,$dbl); |
37 | 88 } |
89 } | |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
90 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
|
91 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
|
92 } |
53
dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents:
49
diff
changeset
|
93 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
|
94 $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
|
95 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
96 throw new Exception($code); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
97 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
98 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
99 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
|
100 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
|
101 $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
|
102 $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
|
103 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
|
104 } |
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
|
105 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
|
106 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
|
107 } |
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
|
108 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
|
109 //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
|
110 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
|
111 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
112 } |
38 | 113 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
|
114 // 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
|
115 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
|
116 $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
|
117 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
118 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
119 throw new Exception("ThrottledRepeatedly"); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
120 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
121 ?> |