Mercurial > hg > ywww
comparison xml/doAmazonRequest.inc @ 35:86f79bc1d142
refactor to split out shareable fn to actually do an Amazon API request
author | Charlie Root |
---|---|
date | Fri, 04 Jan 2019 12:54:12 -0500 |
parents | |
children | 633402a4995d |
comparison
equal
deleted
inserted
replaced
34:5cae8d572998 | 35:86f79bc1d142 |
---|---|
1 <?php | |
2 | |
3 function doAmazonRequest($ext, $parameters, $try) | |
4 { | |
5 $public_key ="AKIAIHTNWC7L6LOUY4LQ"; | |
6 $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde"; | |
7 $requestURI = $_SERVER['REQUEST_URI']; | |
8 $requestIP = $_SERVER['REMOTE_ADDR']; | |
9 $file_data=$ext; | |
10 ksort($parameters); | |
11 foreach ($parameters as $i=>$d) { | |
12 $file_data.='&'.$i.'='.$d; | |
13 } | |
14 //file_put_contents('/var/ywww/debug/phpDebug', | |
15 // "nai1: $requestIP $file_data\n",FILE_APPEND); | |
16 $gotit=0; | |
17 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); | |
18 $crl = curl_init(); | |
19 $timeout = 5; | |
20 curl_setopt ($crl, CURLOPT_URL,$url); | |
21 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); | |
22 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | |
23 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | |
24 for ($i=1; $i<=$try; $i++) { | |
25 $semaphore = new SyncSemaphore("Amazon"); | |
26 $gotit = $semaphore->lock(1000); | |
27 if (!$gotit) { | |
28 file_put_contents('/var/ywww/debug/phpDebug', | |
29 "Got it: ".(int)$gotit." $requestIP 1\n",FILE_APPEND); | |
30 $gotit=$semaphore->lock(1000); | |
31 file_put_contents('/var/ywww/debug/phpDebug', | |
32 "Got it: ".(int)$gotit." $requestIP 1a\n",FILE_APPEND); | |
33 } | |
34 $output = curl_exec($crl); | |
35 //file_put_contents('/var/ywww/debug/phpDebug', | |
36 // "nai2: ".strlen($output)."\n",FILE_APPEND); | |
37 | |
38 curl_close($crl); | |
39 usleep(500000); | |
40 if ($gotit) { | |
41 $semaphore->unlock(); | |
42 } | |
43 else { | |
44 file_put_contents('/var/ywww/debug/phpDebug', | |
45 "W/o lock for $requestIP 1b\n",FILE_APPEND); | |
46 } | |
47 $mm=array(); | |
48 $xml = new SimpleXMLElement($output); | |
49 if (preg_match("/<Error>/",$output,$mm)) { | |
50 $resName=$xml->getName(); | |
51 $code=$xml->Error->Code; | |
52 if ($code) { | |
53 $message=$xml->Error->Message; | |
54 } | |
55 else { | |
56 $code=$xml->Items->Request->Errors->Error->Code; | |
57 $message=$xml->Items->Request->Errors->Error->Message; | |
58 } | |
59 file_put_contents('/var/ywww/debug/phpDebug',"Losing $i: ".$mm[0].", $resName, $code, $requestIP, $requestURI\n",FILE_APPEND); | |
60 if ($code!='RequestThrottled') { | |
61 file_put_contents('/var/ywww/debug/phpDebug',"message: $message\n". | |
62 print_r($parameters,TRUE)."\n",FILE_APPEND); | |
63 if ($code=="") { | |
64 file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$output\n",FILE_APPEND); | |
65 } | |
66 throw new Exception($code); | |
67 } | |
68 } | |
69 else { | |
70 return $xml; | |
71 } | |
72 usleep(200000); // Try to reduce throttling until we get a | |
73 // principled solution in place | |
74 } | |
75 throw new Exception("ThrottledRepeatedly"); | |
76 } | |
77 ?> |