Mercurial > hg > ywww
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xml/doAmazonRequest.php Sun Jan 06 07:17:03 2019 -0500 @@ -0,0 +1,94 @@ +<?php +include_once "ywww/dlog.php"; + +include_once "private/web.php"; +include "aws_signed_request.php"; + +function doAmazonRequest($ext, $parameters, $try, $dbl='x', $wantXML=True) +{ + global $public_key, $private_key; + $requestURI = $_SERVER['REQUEST_URI']; + $requestIP = $_SERVER['REMOTE_ADDR']; + $file_data=$ext; + ksort($parameters); + foreach ($parameters as $i=>$d) { + $file_data.='&'.$i.'='.$d; + } + $gotit=0; + $url=aws_signed_request($ext,$parameters,$public_key,$private_key); + //dl("search: $ext $public_key $private_key\n$url\n".print_r($parameters,TRUE)."\n"); + for ($i=1; $i<=$try; $i++) { + $crl = curl_init(); + $timeout = 5; + curl_setopt ($crl, CURLOPT_URL,$url); + curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); + curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); + $semaphore = new SyncSemaphore("Amazon"); + $gotit = $semaphore->lock(1000); + if (!$gotit) { + dl("Got it: ".(int)$gotit." $requestIP $dbl\n"); + $gotit=$semaphore->lock(1000); + dl("Got it: ".(int)$gotit." $requestIP $dbl"."a\n"); + } + $output = curl_exec($crl); + //dl("nai2: ".strlen($output)."\n"); + + curl_close($crl); + usleep(500000); + if ($gotit) { + $semaphore->unlock(); + } + else { + dl("W/o lock for $requestIP $dbl"."b\n"); + } + $mm=array(); + $xml = new SimpleXMLElement($output); + if (preg_match("/<Error>/",$output,$mm)) { + $resName=$xml->getName(); + $code=$xml->Error->Code; + if ($code) { + $message=$xml->Error->Message; + } + else { + $code=$xml->Items->Request->Errors->Error->Code; + $message=$xml->Items->Request->Errors->Error->Message; + } + dl("Losing $i: $dbl ". + $mm[0].", $resName, $code, $requestIP, $requestURI\n"); + if ($code!='RequestThrottled') { + dl("message: $message\n"); + if ($code=='AWS.InvalidParameterValue' && strpos($message,"for ItemId.")>0) { + // Check for common problem and try to fix... + $spd=$parameters['ItemId']; + if (strpos($spd,'/')>0) { + $isbnMaybe=substr($spd,0,strpos($spd,'/')); + $parameters['ItemId']=$isbnMaybe; + if (isset($parameters['Keywords'])) { + $parameters['Keywords']=urlencode($isbnMaybe); + } + dl("retrying with $isbnMaybe\n"); + return doAmazonRequest($ext,$parameters,$try,$dbl); + } + } + if ($code=="") { + dl("error elt:\n$output\n"); + } + throw new Exception($code); + } + } + else { + if ($wantXML) { + return $xml; + } + else { + //dl("returning ".strlen($output)."\n"); + return $output; + } + } + usleep(100000*$i); // Try to reduce throttling until we get a + // principled solution in place + } + throw new Exception("ThrottledRepeatedly"); +} +?>