diff xml/amazonBookSearch.php @ 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 b1bbf44c701b
children 38d209611508
line wrap: on
line diff
--- a/xml/amazonBookSearch.php	Sat Jan 05 06:21:12 2019 -0500
+++ b/xml/amazonBookSearch.php	Sat Jan 05 18:00:10 2019 -0500
@@ -1,5 +1,9 @@
 <?php
 
+include_once "dlog.php";
+
+include_once "doAmazonRequest.inc";
+
 $Adefault=array(
   'language'           =>'en',           //what language to render the page in
   'locale'             =>'us',           //which server's products? available: ca,de,fr,jp,uk,us
@@ -12,51 +16,6 @@
   'show_url'           =>false,          //debug: show XML request url to be send to Amazon?
 );
 
-$Aassociates_id=array(
-  'uk' => 'bookwhack-21',
-  'us' => 'your02b-20',
-  'ca' => 'book009-20',
-  'de' => 'book04c-21',
-  'fr' => 'book07f-21',
-);
-
-$Aserver=array(
-  'ca' => array(
-    'ext' => 'ca'                      ,  //Canadian normal server
-    'nor' => 'http://www.amazon.ca'    ,  //Canadian normal server
-    'xml' => 'http://xml.amazon.com'   ,  //Canadian xml server
-  ),
-  'de' => array(
-    'ext' => 'de'                      ,  //German normal server
-    'nor' => 'http://www.amazon.de'    ,  //German normal server
-    'xml' => 'http://xml-eu.amazon.com',  //German xml server
-  ),
-  'fr' => array(
-    'ext' => 'fr'                      ,  //French normal server
-    'nor' => 'http://www.amazon.fr'    ,  //French normal server
-    'xml' => 'http://xml-eu.amazon.com',  //French xml server
-  ),
-  'jp' => array(
-    'ext' => 'jp'                      ,  //Japanese normal server, not co.jp!
-    'nor' => 'http://www.amazon.co.jp' ,  //Japanese normal server
-    'xml' => 'http://xml.amazon.com'   ,  //Japanese xml server
-  ),
-  'uk' => array(
-    'ext' => 'co.uk'                   ,  //UK normal server
-    'nor' => 'http://www.amazon.co.uk' ,  //UK normal server
-    'xml' => 'http://xml-eu.amazon.com',  //UK xml server
-  ),
-  'us' => array(
-    'ext' => 'com'                     ,  //USA normal server
-    'nor' => 'http://www.amazon.com'   ,  //USA normal server
-    'xml' => 'http://xml.amazon.com'   ,  //USA xml server
-  ),
-);
-
-include "aws_signed_request.php";  
-$public_key ="AKIAIHTNWC7L6LOUY4LQ";
-$private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde";
-
 //for all parameters see if the user has overruled it or use the default
 foreach ($Adefault as $i=>$d) {
   $$i=isset($_GET[$i])?$_GET[$i]:$d;
@@ -77,39 +36,17 @@
     'ResponseGroup'   =>'Images,ItemAttributes'                ,  //Small, Medium, Large or SellerListing
   );
 
-$ext=$Aserver[$locale]['ext'];  
-$file_data=$ext;
-ksort($parameters);
-foreach ($parameters as $i=>$d) {
-  $file_data.='&'.$i.'='.$d;
+
+try {
+  $ret=doAmazonRequest($Aserver[$locale]['ext'],$parameters,3,'s',False);
 }
-
-$url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
-//echo $url;
-
-        $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);
-	$gotit=0;
-	  $semaphore = new SyncSemaphore("Amazon");
-	  $gotit = $semaphore->lock(1000);
-          if (!$gotit) {
-	    file_put_contents('/var/ywww/debug/phpDebug',
-			      "Got it: ".(int)$gotit." 3\n",FILE_APPEND);
-	    $gotit=$semaphore->lock(1000);
-	    file_put_contents('/var/ywww/debug/phpDebug',
-			      "Got it: ".(int)$gotit." 3a\n",FILE_APPEND);
-	  }
-        $ret = curl_exec($crl);
-        curl_close($crl);
-        usleep(500000);
-	if ($gotit) {
-	  $semaphore->unlock();
-	}
-        else {
-	  file_put_contents('/var/ywww/debug/phpDebug',"W/o lock 3b\n",FILE_APPEND);
-        }
-        echo $ret;
+catch (Exception $e) {
+  if ($e->getMessage()=='AWS.ECommerceService.NoExactMatches') {
+    // Seems to be an 'OK' thing...
+    $ret='<?xml version="1.0" ?><ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"><OperationRequest><RequestId>0e41c74c-eeff-4f24-a761-9537cfebea21</RequestId></OperationRequest><Items><Request><IsValid>True</IsValid></Request><TotalResults>0</TotalResults><TotalPages>0</TotalPages></Items></ItemSearchResponse>';
+  }
+  else {
+    $ret='<?xml version="1.0" ?><ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"><OperationRequest><RequestId>0e41c74c-eeff-4f24-a761-9537cfebea21</RequestId></OperationRequest><Items><Request><IsValid>True</IsValid><Errors><Error><Code>'.$code.'</Code><Message>[lost]</Message></Error></Errors></Request><TotalResults>0</TotalResults><TotalPages>0</TotalPages></Items></ItemSearchResponse>';
+  }
+}
+echo $ret;