Mercurial > hg > ywww
comparison xml/doAmazonRequest.inc @ 43:dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
use dl(...) for debug logging, defined in dlog.php
use doAmazonRequest in amazonBookSearch
use ../private/web.php (q.v., not in mercurial) for Aserver and Aassociates
Started by updating from 40:c24ae74bf6d5, i.e. just before the bug on the main line
author | Charlie Root |
---|---|
date | Sat, 05 Jan 2019 18:00:10 -0500 |
parents | c24ae74bf6d5 |
children |
comparison
equal
deleted
inserted
replaced
42:3f400072bf14 | 43:dbc006408d2b |
---|---|
1 <?php | 1 <?php |
2 include_once "dlog.php"; | |
2 | 3 |
3 function doAmazonRequest($ext, $parameters, $try, $dbl='x') | 4 include_once "web.php"; |
5 include "aws_signed_request.php"; | |
6 | |
7 function doAmazonRequest($ext, $parameters, $try, $dbl='x', $wantXML=True) | |
4 { | 8 { |
5 $public_key ="AKIAIHTNWC7L6LOUY4LQ"; | 9 global $public_key, $private_key; |
6 $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde"; | |
7 $requestURI = $_SERVER['REQUEST_URI']; | 10 $requestURI = $_SERVER['REQUEST_URI']; |
8 $requestIP = $_SERVER['REMOTE_ADDR']; | 11 $requestIP = $_SERVER['REMOTE_ADDR']; |
9 $file_data=$ext; | 12 $file_data=$ext; |
10 ksort($parameters); | 13 ksort($parameters); |
11 foreach ($parameters as $i=>$d) { | 14 foreach ($parameters as $i=>$d) { |
12 $file_data.='&'.$i.'='.$d; | 15 $file_data.='&'.$i.'='.$d; |
13 } | 16 } |
14 //file_put_contents('/var/ywww/debug/phpDebug', | |
15 // "nai1: $requestIP $file_data\n",FILE_APPEND); | |
16 $gotit=0; | 17 $gotit=0; |
17 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); | 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"); | |
18 for ($i=1; $i<=$try; $i++) { | 20 for ($i=1; $i<=$try; $i++) { |
19 $crl = curl_init(); | 21 $crl = curl_init(); |
20 $timeout = 5; | 22 $timeout = 5; |
21 curl_setopt ($crl, CURLOPT_URL,$url); | 23 curl_setopt ($crl, CURLOPT_URL,$url); |
22 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); | 24 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); |
23 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | 25 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); |
24 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | 26 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); |
25 $semaphore = new SyncSemaphore("Amazon"); | 27 $semaphore = new SyncSemaphore("Amazon"); |
26 $gotit = $semaphore->lock(1000); | 28 $gotit = $semaphore->lock(1000); |
27 if (!$gotit) { | 29 if (!$gotit) { |
28 file_put_contents('/var/ywww/debug/phpDebug', | 30 dl("Got it: ".(int)$gotit." $requestIP $dbl\n"); |
29 "Got it: ".(int)$gotit." $requestIP $dbl\n",FILE_APPEND); | |
30 $gotit=$semaphore->lock(1000); | 31 $gotit=$semaphore->lock(1000); |
31 file_put_contents('/var/ywww/debug/phpDebug', | 32 dl("Got it: ".(int)$gotit." $requestIP $dbl"."a\n"); |
32 "Got it: ".(int)$gotit." $requestIP $dbl"."a\n",FILE_APPEND); | |
33 } | 33 } |
34 $output = curl_exec($crl); | 34 $output = curl_exec($crl); |
35 //file_put_contents('/var/ywww/debug/phpDebug', | 35 //dl("nai2: ".strlen($output)."\n"); |
36 // "nai2: ".strlen($output)."\n",FILE_APPEND); | |
37 | 36 |
38 curl_close($crl); | 37 curl_close($crl); |
39 usleep(500000); | 38 usleep(500000); |
40 if ($gotit) { | 39 if ($gotit) { |
41 $semaphore->unlock(); | 40 $semaphore->unlock(); |
42 } | 41 } |
43 else { | 42 else { |
44 file_put_contents('/var/ywww/debug/phpDebug', | 43 dl("W/o lock for $requestIP $dbl"."b\n"); |
45 "W/o lock for $requestIP $dbl"."b\n",FILE_APPEND); | |
46 } | 44 } |
47 $mm=array(); | 45 $mm=array(); |
48 $xml = new SimpleXMLElement($output); | 46 $xml = new SimpleXMLElement($output); |
49 if (preg_match("/<Error>/",$output,$mm)) { | 47 if (preg_match("/<Error>/",$output,$mm)) { |
50 $resName=$xml->getName(); | 48 $resName=$xml->getName(); |
54 } | 52 } |
55 else { | 53 else { |
56 $code=$xml->Items->Request->Errors->Error->Code; | 54 $code=$xml->Items->Request->Errors->Error->Code; |
57 $message=$xml->Items->Request->Errors->Error->Message; | 55 $message=$xml->Items->Request->Errors->Error->Message; |
58 } | 56 } |
59 file_put_contents('/var/ywww/debug/phpDebug',"Losing $i: $dbl ".$mm[0].", $resName, $code, $requestIP, $requestURI\n",FILE_APPEND); | 57 dl("Losing $i: $dbl ". |
58 $mm[0].", $resName, $code, $requestIP, $requestURI\n"); | |
60 if ($code!='RequestThrottled') { | 59 if ($code!='RequestThrottled') { |
61 file_put_contents('/var/ywww/debug/phpDebug',"message: $message\n",FILE_APPEND); | 60 dl("message: $message\n"); |
62 if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) { | 61 if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) { |
63 // Check for common problem and try to fix... | 62 // Check for common problem and try to fix... |
64 $spd=$parameters['ItemId']; | 63 $spd=$parameters['ItemId']; |
65 if (strpos($spd,'/')>0) { | 64 if (strpos($spd,'/')>0) { |
66 $isbnMaybe=substr($spd,0,strpos($spd,'/')); | 65 $isbnMaybe=substr($spd,0,strpos($spd,'/')); |
67 $parameters['ItemId']=$isbnMaybe; | 66 $parameters['ItemId']=$isbnMaybe; |
68 if (isset($parameters['Keywords'])) { | 67 if (isset($parameters['Keywords'])) { |
69 $parameters['Keywords']=urlencode($isbnMaybe); | 68 $parameters['Keywords']=urlencode($isbnMaybe); |
70 } | 69 } |
71 file_put_contents('/var/ywww/debug/phpDebug',"retrying with $isbnMaybe\n",FILE_APPEND); | 70 dl("retrying with $isbnMaybe\n"); |
72 return doAmazonRequest($ext,$parameters,$try,$dbl); | 71 return doAmazonRequest($ext,$parameters,$try,$dbl); |
73 } | 72 } |
74 } | 73 } |
75 if ($code=="") { | 74 if ($code=="") { |
76 file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$output\n",FILE_APPEND); | 75 dl("error elt:\n$output\n"); |
77 } | 76 } |
78 throw new Exception($code); | 77 throw new Exception($code); |
79 } | 78 } |
80 } | 79 } |
81 else { | 80 else { |
82 return $xml; | 81 if ($wantXML) { |
82 return $xml; | |
83 } | |
84 else { | |
85 //dl("returning ".strlen($output)."\n"); | |
86 return $output; | |
87 } | |
83 } | 88 } |
84 usleep(100000*$i); // Try to reduce throttling until we get a | 89 usleep(100000*$i); // Try to reduce throttling until we get a |
85 // principled solution in place | 90 // principled solution in place |
86 } | 91 } |
87 throw new Exception("ThrottledRepeatedly"); | 92 throw new Exception("ThrottledRepeatedly"); |