view xml/doAmazonRequest.inc @ 38:c24ae74bf6d5

pass char for log lines in to doAmazonRequest
author Charlie Root
date Fri, 04 Jan 2019 18:02:20 -0500
parents 633402a4995d
children dbc006408d2b
line wrap: on
line source

<?php

function doAmazonRequest($ext, $parameters, $try, $dbl='x')
{
  $public_key ="AKIAIHTNWC7L6LOUY4LQ";
  $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde";
  $requestURI = $_SERVER['REQUEST_URI'];
  $requestIP = $_SERVER['REMOTE_ADDR'];
  $file_data=$ext;
  ksort($parameters);
  foreach ($parameters as $i=>$d) {
    $file_data.='&'.$i.'='.$d;
  }
  //file_put_contents('/var/ywww/debug/phpDebug',
  //		"nai1: $requestIP $file_data\n",FILE_APPEND);
  $gotit=0;
  $url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
  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) {
      file_put_contents('/var/ywww/debug/phpDebug',
			"Got it: ".(int)$gotit." $requestIP $dbl\n",FILE_APPEND);
      $gotit=$semaphore->lock(1000);
      file_put_contents('/var/ywww/debug/phpDebug',
			"Got it: ".(int)$gotit." $requestIP $dbl"."a\n",FILE_APPEND);
    }
    $output = curl_exec($crl);
    //file_put_contents('/var/ywww/debug/phpDebug',
    //	      "nai2: ".strlen($output)."\n",FILE_APPEND);

    curl_close($crl);
    usleep(500000);
    if ($gotit) {
      $semaphore->unlock();
    }
    else {
      file_put_contents('/var/ywww/debug/phpDebug',
			"W/o lock for $requestIP $dbl"."b\n",FILE_APPEND);
    }
    $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;
      }
      file_put_contents('/var/ywww/debug/phpDebug',"Losing $i: $dbl ".$mm[0].", $resName, $code, $requestIP, $requestURI\n",FILE_APPEND);
      if ($code!='RequestThrottled') {
	file_put_contents('/var/ywww/debug/phpDebug',"message: $message\n",FILE_APPEND);
	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);
	      }
	    file_put_contents('/var/ywww/debug/phpDebug',"retrying with $isbnMaybe\n",FILE_APPEND);
	    return doAmazonRequest($ext,$parameters,$try,$dbl);
	  }
	}
	if ($code=="") {
	  file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$output\n",FILE_APPEND);
	}
	throw new Exception($code);
      }
    }
    else {
      return $xml;
    }
    usleep(100000*$i); // Try to reduce throttling until we get a 
    // principled solution in place
  }
  throw new Exception("ThrottledRepeatedly");
}
?>