Mercurial > hg > ywww
view 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 |
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"); } ?>