annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
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
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
4 include_once "web.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
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
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
21 $crl = curl_init();
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
22 $timeout = 5;
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
23 curl_setopt ($crl, CURLOPT_URL,$url);
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
24 curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
25 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
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
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
61 if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) {
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
62 // Check for common problem and try to fix...
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
63 $spd=$parameters['ItemId'];
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
64 if (strpos($spd,'/')>0) {
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
65 $isbnMaybe=substr($spd,0,strpos($spd,'/'));
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
66 $parameters['ItemId']=$isbnMaybe;
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
67 if (isset($parameters['Keywords'])) {
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
68 $parameters['Keywords']=urlencode($isbnMaybe);
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
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("retrying with $isbnMaybe\n");
38
c24ae74bf6d5 pass char for log lines in to doAmazonRequest
Charlie Root
parents: 37
diff changeset
71 return doAmazonRequest($ext,$parameters,$try,$dbl);
37
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
72 }
633402a4995d try to fix a common lookup error
Charlie Root
parents: 35
diff changeset
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
c24ae74bf6d5 pass char for log lines in to doAmazonRequest
Charlie Root
parents: 37
diff changeset
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 ?>