changeset 5:55445b456ad0

short-circuit error response handling, add debugging
author robert
date Wed, 28 Dec 2016 13:07:32 +0000
parents a6b55ae70617
children 077b0a0a3e6d
files xml/getAmazonInfo.php
diffstat 1 files changed, 42 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/xml/getAmazonInfo.php	Wed Dec 28 13:04:53 2016 +0000
+++ b/xml/getAmazonInfo.php	Wed Dec 28 13:07:32 2016 +0000
@@ -1,5 +1,6 @@
 <?php
 
+$lastReqTime=0;
 function getAmazonDet($isbn,$go,$localeIn)
 {
   global $output;
@@ -93,7 +94,7 @@
 
 $url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
 //echo $url;
- file_put_contents('/var/ywww/debug/phpDebug',"parms: ".
+ file_put_contents('/var/ywww/debug/phpDebug',"parms: $go".
 		   print_r($parameters,TRUE)."\n",FILE_APPEND);
         $crl = curl_init();
         $timeout = 5;
@@ -101,30 +102,47 @@
 		curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 
         curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
+	/*	$timeDiff=((int)(microtime(true)*1000))-$lastReqTime;
+	if ($timeDiff<1000) {
+	  usleep((1000-$timeDiff)*1000); // microseconds
+	  }*/
         $output = curl_exec($crl);
+	//$lastReqTime=(int)(microtime(true)*1000); // milliseconds
         curl_close($crl);
 		
 	/*$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><ItemLookupErrorResponse xmlns=\"http://ecs.amazonaws.com/doc/2009-03-31/\"><Error><Code>AccountLimitExceeded</Code><Message>Account limit of 2056 requests per hour exceeded.</Message></Error><RequestID>290ed059-730c-4789-93b4-6d21e11053d3</RequestID></ItemLookupErrorResponse>");*/
-	$xml = new SimpleXMLElement($output);	
 	$review = "";
-	set_error_handler(function () {
-	    global $output;
-	    file_put_contents('/var/ywww/debug/phpDebug', "Caught one: ",FILE_APPEND);
-	    file_put_contents('/var/ywww/debug/phpDebug',
-			      print_r($output, TRUE)."\n",
-			      FILE_APPEND);
+	// HST added this
+	if (preg_match("<Error>",$output)) {
+	  $xml = new SimpleXMLElement($output);	
+	  $resName=$xml->getName();
+	  $code=$xml->Error->Code;
+	  file_put_contents('/var/ywww/debug/phpDebug',"Losing: $resName, ".
+			    $xml->Error->Code."\n",FILE_APPEND);
+	}
+	else {
+	  $xml = new SimpleXMLElement($output);	
+	  set_error_handler(function () {
+	      global $output;
+	      file_put_contents('/var/ywww/debug/phpDebug',
+				"Caught one?: ".$searchparameterdata,
+				FILE_APPEND);
+	      file_put_contents('/var/ywww/debug/phpDebug',
+				print_r($output, TRUE)."\n",
+				FILE_APPEND);
 	    } );
-	$review = $xml->Items->Item->CustomerReviews->IFrameURL;
-	// The above is failing repeatedly -- 
-        //PHP Notice: Trying to get property of non-object in
-	// /var/ywww/xml/getAmazonInfo.php on line [109]
-	// See the dumped structure at the end of this file for the
-	// cause
-	restore_error_handler();
-	//echo $review;
-	$review1 = "";
-	$review2 = "";
-	$review3 = "";
+	  $review = $xml->Items->Item->CustomerReviews->IFrameURL;
+	  // The above is failing repeatedly -- 
+	  //PHP Notice: Trying to get property of non-object in
+	  // /var/ywww/xml/getAmazonInfo.php on line [109]
+	  // See the dumped structure at the end of this file for the
+	  // cause
+	  restore_error_handler();
+	  //echo $review;
+	  $review1 = "";
+	  $review2 = "";
+	  $review3 = "";
+	}
 	if ($review != "")
 	{
 		$text = @file_get_contents($review . "&truncate=300");
@@ -235,8 +253,12 @@
 
 	if($errorCode != "AccountLimitExceeded")
 	{
-		if($go == 1)
+	  if($go == 1) {
+	    file_put_contents('/var/ywww/debug/phpDebug',"win: ".
+			    $xml->Items->Item->ItemAttributes->Title."\n",
+			      FILE_APPEND);
 			return $output;
+	  }
 		else
 		{
 			$title = $xml->Items->Item->ItemAttributes->Title;