comparison 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
comparison
equal deleted inserted replaced
42:3f400072bf14 43:dbc006408d2b
1 <?php 1 <?php
2
3 include_once "dlog.php";
4
5 include_once "doAmazonRequest.inc";
2 6
3 $Adefault=array( 7 $Adefault=array(
4 'language' =>'en', //what language to render the page in 8 'language' =>'en', //what language to render the page in
5 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us 9 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us
6 'page' =>1, //first page to show (we are counting from 1 not 0) 10 'page' =>1, //first page to show (we are counting from 1 not 0)
9 'searchparameter' =>'Keywords', //what kind of search? 13 'searchparameter' =>'Keywords', //what kind of search?
10 'searchparameterdata'=>'Machiavelli', //what to search for? 14 'searchparameterdata'=>'Machiavelli', //what to search for?
11 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true' 15 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true'
12 'show_url' =>false, //debug: show XML request url to be send to Amazon? 16 'show_url' =>false, //debug: show XML request url to be send to Amazon?
13 ); 17 );
14
15 $Aassociates_id=array(
16 'uk' => 'bookwhack-21',
17 'us' => 'your02b-20',
18 'ca' => 'book009-20',
19 'de' => 'book04c-21',
20 'fr' => 'book07f-21',
21 );
22
23 $Aserver=array(
24 'ca' => array(
25 'ext' => 'ca' , //Canadian normal server
26 'nor' => 'http://www.amazon.ca' , //Canadian normal server
27 'xml' => 'http://xml.amazon.com' , //Canadian xml server
28 ),
29 'de' => array(
30 'ext' => 'de' , //German normal server
31 'nor' => 'http://www.amazon.de' , //German normal server
32 'xml' => 'http://xml-eu.amazon.com', //German xml server
33 ),
34 'fr' => array(
35 'ext' => 'fr' , //French normal server
36 'nor' => 'http://www.amazon.fr' , //French normal server
37 'xml' => 'http://xml-eu.amazon.com', //French xml server
38 ),
39 'jp' => array(
40 'ext' => 'jp' , //Japanese normal server, not co.jp!
41 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server
42 'xml' => 'http://xml.amazon.com' , //Japanese xml server
43 ),
44 'uk' => array(
45 'ext' => 'co.uk' , //UK normal server
46 'nor' => 'http://www.amazon.co.uk' , //UK normal server
47 'xml' => 'http://xml-eu.amazon.com', //UK xml server
48 ),
49 'us' => array(
50 'ext' => 'com' , //USA normal server
51 'nor' => 'http://www.amazon.com' , //USA normal server
52 'xml' => 'http://xml.amazon.com' , //USA xml server
53 ),
54 );
55
56 include "aws_signed_request.php";
57 $public_key ="AKIAIHTNWC7L6LOUY4LQ";
58 $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde";
59 18
60 //for all parameters see if the user has overruled it or use the default 19 //for all parameters see if the user has overruled it or use the default
61 foreach ($Adefault as $i=>$d) { 20 foreach ($Adefault as $i=>$d) {
62 $$i=isset($_GET[$i])?$_GET[$i]:$d; 21 $$i=isset($_GET[$i])?$_GET[$i]:$d;
63 } 22 }
75 'Availability' =>'Available', 34 'Availability' =>'Available',
76 'Condition' =>'All', 35 'Condition' =>'All',
77 'ResponseGroup' =>'Images,ItemAttributes' , //Small, Medium, Large or SellerListing 36 'ResponseGroup' =>'Images,ItemAttributes' , //Small, Medium, Large or SellerListing
78 ); 37 );
79 38
80 $ext=$Aserver[$locale]['ext']; 39
81 $file_data=$ext; 40 try {
82 ksort($parameters); 41 $ret=doAmazonRequest($Aserver[$locale]['ext'],$parameters,3,'s',False);
83 foreach ($parameters as $i=>$d) {
84 $file_data.='&'.$i.'='.$d;
85 } 42 }
86 43 catch (Exception $e) {
87 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); 44 if ($e->getMessage()=='AWS.ECommerceService.NoExactMatches') {
88 //echo $url; 45 // Seems to be an 'OK' thing...
89 46 $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>';
90 $crl = curl_init(); 47 }
91 $timeout = 5; 48 else {
92 curl_setopt ($crl, CURLOPT_URL,$url); 49 $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>';
93 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 50 }
94 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 51 }
95 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 52 echo $ret;
96 $gotit=0;
97 $semaphore = new SyncSemaphore("Amazon");
98 $gotit = $semaphore->lock(1000);
99 if (!$gotit) {
100 file_put_contents('/var/ywww/debug/phpDebug',
101 "Got it: ".(int)$gotit." 3\n",FILE_APPEND);
102 $gotit=$semaphore->lock(1000);
103 file_put_contents('/var/ywww/debug/phpDebug',
104 "Got it: ".(int)$gotit." 3a\n",FILE_APPEND);
105 }
106 $ret = curl_exec($crl);
107 curl_close($crl);
108 usleep(500000);
109 if ($gotit) {
110 $semaphore->unlock();
111 }
112 else {
113 file_put_contents('/var/ywww/debug/phpDebug',"W/o lock 3b\n",FILE_APPEND);
114 }
115 echo $ret;