view xml/doAmazonRequest.inc @ 35:86f79bc1d142

refactor to split out shareable fn to actually do an Amazon API request
author Charlie Root
date Fri, 04 Jan 2019 12:54:12 -0500
parents
children 633402a4995d
line wrap: on
line source

<?php

function doAmazonRequest($ext, $parameters, $try)
{
  $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);  
  $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);
  for ($i=1; $i<=$try; $i++) {
    $semaphore = new SyncSemaphore("Amazon");
    $gotit = $semaphore->lock(1000);
    if (!$gotit) {
      file_put_contents('/var/ywww/debug/phpDebug',
			"Got it: ".(int)$gotit." $requestIP 1\n",FILE_APPEND);
      $gotit=$semaphore->lock(1000);
      file_put_contents('/var/ywww/debug/phpDebug',
			"Got it: ".(int)$gotit." $requestIP 1a\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 1b\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: ".$mm[0].", $resName, $code, $requestIP, $requestURI\n",FILE_APPEND);
      if ($code!='RequestThrottled') {
	file_put_contents('/var/ywww/debug/phpDebug',"message: $message\n".
			  print_r($parameters,TRUE)."\n",FILE_APPEND);
	if ($code=="") {
	  file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$output\n",FILE_APPEND);
	}
	throw new Exception($code);
      }
    }
    else {
      return $xml;
    }
    usleep(200000); // Try to reduce throttling until we get a 
    // principled solution in place
  }
  throw new Exception("ThrottledRepeatedly");
}
?>