Mercurial > hg > ywww
annotate xml/doAmazonRequest.php @ 44:38d209611508
change includes to assume (contra previos ASSUMES) that we have .user.ini which adds /var/test to include_path
author | Charlie Root |
---|---|
date | Sun, 06 Jan 2019 07:17:03 -0500 |
parents | xml/doAmazonRequest.inc@dbc006408d2b |
children | a67bf725e87b |
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 |
44
38d209611508
change includes to assume (contra previos ASSUMES) that we have .user.ini which adds /var/test to include_path
Charlie Root
parents:
43
diff
changeset
|
2 include_once "ywww/dlog.php"; |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
3 |
44
38d209611508
change includes to assume (contra previos ASSUMES) that we have .user.ini which adds /var/test to include_path
Charlie Root
parents:
43
diff
changeset
|
4 include_once "private/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++) { |
37 | 21 $crl = curl_init(); |
22 $timeout = 5; | |
23 curl_setopt ($crl, CURLOPT_URL,$url); | |
24 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); | |
25 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | |
26 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
27 $semaphore = new SyncSemaphore("Amazon"); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
28 $gotit = $semaphore->lock(1000); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
29 if (!$gotit) { |
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
|
30 dl("Got it: ".(int)$gotit." $requestIP $dbl\n"); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
31 $gotit=$semaphore->lock(1000); |
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
|
32 dl("Got it: ".(int)$gotit." $requestIP $dbl"."a\n"); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
33 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
34 $output = curl_exec($crl); |
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
|
35 //dl("nai2: ".strlen($output)."\n"); |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
36 |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
37 curl_close($crl); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
38 usleep(500000); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
39 if ($gotit) { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
40 $semaphore->unlock(); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
41 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
42 else { |
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
|
43 dl("W/o lock for $requestIP $dbl"."b\n"); |
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 $mm=array(); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
46 $xml = new SimpleXMLElement($output); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
47 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
|
48 $resName=$xml->getName(); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
49 $code=$xml->Error->Code; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
50 if ($code) { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
51 $message=$xml->Error->Message; |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
52 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
53 else { |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
54 $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
|
55 $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
|
56 } |
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
|
57 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
|
58 $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
|
59 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
|
60 dl("message: $message\n"); |
37 | 61 if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) { |
62 // Check for common problem and try to fix... | |
63 $spd=$parameters['ItemId']; | |
64 if (strpos($spd,'/')>0) { | |
65 $isbnMaybe=substr($spd,0,strpos($spd,'/')); | |
66 $parameters['ItemId']=$isbnMaybe; | |
67 if (isset($parameters['Keywords'])) { | |
68 $parameters['Keywords']=urlencode($isbnMaybe); | |
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("retrying with $isbnMaybe\n"); |
38 | 71 return doAmazonRequest($ext,$parameters,$try,$dbl); |
37 | 72 } |
73 } | |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
74 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
|
75 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
|
76 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
77 throw new Exception($code); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
78 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
79 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
80 else { |
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
|
81 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
|
82 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
|
83 } |
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
|
84 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
|
85 //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
|
86 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
|
87 } |
35
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
88 } |
38 | 89 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
|
90 // principled solution in place |
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 throw new Exception("ThrottledRepeatedly"); |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
93 } |
86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
Charlie Root
parents:
diff
changeset
|
94 ?> |