Mercurial > hg > ywww
view xml/doAmazonRequest.php @ 53:dd93cb4b77ad
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
author | Charlie Root |
---|---|
date | Thu, 06 Jun 2019 16:37:20 -0400 |
parents | a67bf725e87b |
children | 72708ec9c0e6 |
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++) { dl("dar: $requestIP $i\n"); $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".$ext); $gotit = $semaphore->lock(5000); if (!$gotit) { dl("No lock: $requestIP $dbl\n"); for ($j=1; $j<=$try+4; $j++) { dl("dar: $requestIP $i $j\n"); $gotit=$semaphore->lock(1000); if ($gotit) { dl("Got it: $i $j $requestIP $dbl"."a\n"); break; } } } else { dl("Got it: $i ! $requestIP $dbl"."a\n"); } if (!$gotit) { usleep(1000000); continue; } usleep(1000000); $output = curl_exec($crl); dl("nai2: $gotit ".strlen($output)."\n"); curl_close($crl); $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"); if ($gotit) { $semaphore->unlock(); } return doAmazonRequest($ext,$parameters,$try,$dbl); } } if ($code=="") { dl("error elt:\n$output\n"); } if ($gotit) { $semaphore->unlock(); } throw new Exception($code); } } else { if ($gotit) { $cnt=0; $semaphore->unlock($cnt); dl("unlocked: $i $cnt\n"); } 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 if ($gotit) { $semaphore->unlock(); } } throw new Exception("ThrottledRepeatedly"); } ?>