annotate xml/amazonBookSearch.php @ 30:b1bbf44c701b

standarise semaphore use
author Robert Boland <robert@markup.co.uk>
date Fri, 04 Jan 2019 08:50:23 -0500
parents d606320ec331
children dbc006408d2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
1 <?php
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
2
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
3 $Adefault=array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
4 'language' =>'en', //what language to render the page in
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
5 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
6 'page' =>1, //first page to show (we are counting from 1 not 0)
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
7 'operation' =>'ItemSearch', //what to do? //ItemSearch
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
8 'searchindex' =>'Books', //what product category for search?
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
9 'searchparameter' =>'Keywords', //what kind of search?
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
10 'searchparameterdata'=>'Machiavelli', //what to search for?
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
11 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true'
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
12 'show_url' =>false, //debug: show XML request url to be send to Amazon?
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
13 );
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
14
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
15 $Aassociates_id=array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
16 'uk' => 'bookwhack-21',
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
17 'us' => 'your02b-20',
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
18 'ca' => 'book009-20',
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
19 'de' => 'book04c-21',
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
20 'fr' => 'book07f-21',
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
21 );
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
22
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
23 $Aserver=array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
24 'ca' => array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
25 'ext' => 'ca' , //Canadian normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
26 'nor' => 'http://www.amazon.ca' , //Canadian normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
27 'xml' => 'http://xml.amazon.com' , //Canadian xml server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
28 ),
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
29 'de' => array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
30 'ext' => 'de' , //German normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
31 'nor' => 'http://www.amazon.de' , //German normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
32 'xml' => 'http://xml-eu.amazon.com', //German xml server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
33 ),
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
34 'fr' => array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
35 'ext' => 'fr' , //French normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
36 'nor' => 'http://www.amazon.fr' , //French normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
37 'xml' => 'http://xml-eu.amazon.com', //French xml server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
38 ),
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
39 'jp' => array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
40 'ext' => 'jp' , //Japanese normal server, not co.jp!
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
41 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
42 'xml' => 'http://xml.amazon.com' , //Japanese xml server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
43 ),
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
44 'uk' => array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
45 'ext' => 'co.uk' , //UK normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
46 'nor' => 'http://www.amazon.co.uk' , //UK normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
47 'xml' => 'http://xml-eu.amazon.com', //UK xml server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
48 ),
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
49 'us' => array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
50 'ext' => 'com' , //USA normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
51 'nor' => 'http://www.amazon.com' , //USA normal server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
52 'xml' => 'http://xml.amazon.com' , //USA xml server
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
53 ),
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
54 );
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
55
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
56 include "aws_signed_request.php";
17
ae1459564f66 Replace old associate keys with new IAMs user keys
Robert Boland <robert@markup.co.uk>
parents: 6
diff changeset
57 $public_key ="AKIAIHTNWC7L6LOUY4LQ";
ae1459564f66 Replace old associate keys with new IAMs user keys
Robert Boland <robert@markup.co.uk>
parents: 6
diff changeset
58 $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde";
6
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
59
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
60 //for all parameters see if the user has overruled it or use the default
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
61 foreach ($Adefault as $i=>$d) {
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
62 $$i=isset($_GET[$i])?$_GET[$i]:$d;
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
63 }
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
64 //this is the data that is used to form the request for AWS
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
65 //this is the part that is search specific
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
66
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
67 $parameters=array(
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
68 'Operation' =>$operation ,
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
69 //'Keywords' =>urlencode($search) ,
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
70 'SearchIndex' =>$searchindex , //Books for example.
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
71 "$searchparameter"=>stripslashes($searchparameterdata),
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
72 'ItemPage' =>$page , //which page?
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
73 'AssociateTag' =>$Aassociates_id[$locale],
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
74 'MerchantId' =>'All',
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
75 'Availability' =>'Available',
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
76 'Condition' =>'All',
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
77 'ResponseGroup' =>'Images,ItemAttributes' , //Small, Medium, Large or SellerListing
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
78 );
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
79
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
80 $ext=$Aserver[$locale]['ext'];
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
81 $file_data=$ext;
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
82 ksort($parameters);
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
83 foreach ($parameters as $i=>$d) {
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
84 $file_data.='&'.$i.'='.$d;
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
85 }
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
86
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
87 $url=aws_signed_request($ext,$parameters,$public_key,$private_key);
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
88 //echo $url;
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
89
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
90 $crl = curl_init();
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
91 $timeout = 5;
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
92 curl_setopt ($crl, CURLOPT_URL,$url);
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
93 curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
94 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
95 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
21
46382face560 use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents: 17
diff changeset
96 $gotit=0;
46382face560 use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents: 17
diff changeset
97 $semaphore = new SyncSemaphore("Amazon");
23
d606320ec331 post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents: 21
diff changeset
98 $gotit = $semaphore->lock(1000);
30
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
99 if (!$gotit) {
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
100 file_put_contents('/var/ywww/debug/phpDebug',
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
101 "Got it: ".(int)$gotit." 3\n",FILE_APPEND);
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
102 $gotit=$semaphore->lock(1000);
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
103 file_put_contents('/var/ywww/debug/phpDebug',
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
104 "Got it: ".(int)$gotit." 3a\n",FILE_APPEND);
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
105 }
6
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
106 $ret = curl_exec($crl);
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
107 curl_close($crl);
30
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
108 usleep(500000);
21
46382face560 use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents: 17
diff changeset
109 if ($gotit) {
46382face560 use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents: 17
diff changeset
110 $semaphore->unlock();
46382face560 use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents: 17
diff changeset
111 }
30
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
112 else {
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
113 file_put_contents('/var/ywww/debug/phpDebug',"W/o lock 3b\n",FILE_APPEND);
b1bbf44c701b standarise semaphore use
Robert Boland <robert@markup.co.uk>
parents: 23
diff changeset
114 }
6
077b0a0a3e6d remaining originals according to dependency walk
Robert Boland <robert@markup.co.uk>
parents:
diff changeset
115 echo $ret;