Mercurial > hg > ywww
view xml/doAmazonRequest.php @ 49:a67bf725e87b
put both paths in include_path and depend on that
author | Charlie Root |
---|---|
date | Wed, 16 Jan 2019 13:42:15 -0500 |
parents | 38d209611508 |
children | dd93cb4b77ad |
line wrap: on
line source
<?php include_once "dlog.php"; include_once "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"); } ?>