Mercurial > hg > ywww
comparison 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 |
comparison
equal
deleted
inserted
replaced
43:dbc006408d2b | 44:38d209611508 |
---|---|
1 <?php | |
2 include_once "ywww/dlog.php"; | |
3 | |
4 include_once "private/web.php"; | |
5 include "aws_signed_request.php"; | |
6 | |
7 function doAmazonRequest($ext, $parameters, $try, $dbl='x', $wantXML=True) | |
8 { | |
9 global $public_key, $private_key; | |
10 $requestURI = $_SERVER['REQUEST_URI']; | |
11 $requestIP = $_SERVER['REMOTE_ADDR']; | |
12 $file_data=$ext; | |
13 ksort($parameters); | |
14 foreach ($parameters as $i=>$d) { | |
15 $file_data.='&'.$i.'='.$d; | |
16 } | |
17 $gotit=0; | |
18 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); | |
19 //dl("search: $ext $public_key $private_key\n$url\n".print_r($parameters,TRUE)."\n"); | |
20 for ($i=1; $i<=$try; $i++) { | |
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); | |
27 $semaphore = new SyncSemaphore("Amazon"); | |
28 $gotit = $semaphore->lock(1000); | |
29 if (!$gotit) { | |
30 dl("Got it: ".(int)$gotit." $requestIP $dbl\n"); | |
31 $gotit=$semaphore->lock(1000); | |
32 dl("Got it: ".(int)$gotit." $requestIP $dbl"."a\n"); | |
33 } | |
34 $output = curl_exec($crl); | |
35 //dl("nai2: ".strlen($output)."\n"); | |
36 | |
37 curl_close($crl); | |
38 usleep(500000); | |
39 if ($gotit) { | |
40 $semaphore->unlock(); | |
41 } | |
42 else { | |
43 dl("W/o lock for $requestIP $dbl"."b\n"); | |
44 } | |
45 $mm=array(); | |
46 $xml = new SimpleXMLElement($output); | |
47 if (preg_match("/<Error>/",$output,$mm)) { | |
48 $resName=$xml->getName(); | |
49 $code=$xml->Error->Code; | |
50 if ($code) { | |
51 $message=$xml->Error->Message; | |
52 } | |
53 else { | |
54 $code=$xml->Items->Request->Errors->Error->Code; | |
55 $message=$xml->Items->Request->Errors->Error->Message; | |
56 } | |
57 dl("Losing $i: $dbl ". | |
58 $mm[0].", $resName, $code, $requestIP, $requestURI\n"); | |
59 if ($code!='RequestThrottled') { | |
60 dl("message: $message\n"); | |
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 } | |
70 dl("retrying with $isbnMaybe\n"); | |
71 return doAmazonRequest($ext,$parameters,$try,$dbl); | |
72 } | |
73 } | |
74 if ($code=="") { | |
75 dl("error elt:\n$output\n"); | |
76 } | |
77 throw new Exception($code); | |
78 } | |
79 } | |
80 else { | |
81 if ($wantXML) { | |
82 return $xml; | |
83 } | |
84 else { | |
85 //dl("returning ".strlen($output)."\n"); | |
86 return $output; | |
87 } | |
88 } | |
89 usleep(100000*$i); // Try to reduce throttling until we get a | |
90 // principled solution in place | |
91 } | |
92 throw new Exception("ThrottledRepeatedly"); | |
93 } | |
94 ?> |