annotate php/read/getBookDetails.php @ 53:dd93cb4b77ad

more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
author Charlie Root
date Thu, 06 Jun 2019 16:37:20 -0400
parents a67bf725e87b
children 39600878ff93
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
45ae2aeb2b52 original
robert
parents:
diff changeset
1 <?php
49
a67bf725e87b put both paths in include_path and depend on that
Charlie Root
parents: 48
diff changeset
2 include_once "dlog.php";
2
45ae2aeb2b52 original
robert
parents:
diff changeset
3
49
a67bf725e87b put both paths in include_path and depend on that
Charlie Root
parents: 48
diff changeset
4 include_once "web.php";
2
45ae2aeb2b52 original
robert
parents:
diff changeset
5 function findGenre($browseNode, &$ID, &$gen)
45ae2aeb2b52 original
robert
parents:
diff changeset
6 {
45ae2aeb2b52 original
robert
parents:
diff changeset
7 if($browseNode->Name == "Subjects")
45ae2aeb2b52 original
robert
parents:
diff changeset
8 {
45ae2aeb2b52 original
robert
parents:
diff changeset
9 return true;
45ae2aeb2b52 original
robert
parents:
diff changeset
10 }
45ae2aeb2b52 original
robert
parents:
diff changeset
11 else
45ae2aeb2b52 original
robert
parents:
diff changeset
12 {
45ae2aeb2b52 original
robert
parents:
diff changeset
13 if($browseNode->Ancestors->BrowseNode)
45ae2aeb2b52 original
robert
parents:
diff changeset
14 {
45ae2aeb2b52 original
robert
parents:
diff changeset
15 if(findGenre($browseNode->Ancestors->BrowseNode, $ID, $gen) == true)
45ae2aeb2b52 original
robert
parents:
diff changeset
16 {
45ae2aeb2b52 original
robert
parents:
diff changeset
17 $gen = $browseNode->Name;
45ae2aeb2b52 original
robert
parents:
diff changeset
18 $ID = $browseNode->BrowseNodeId;
45ae2aeb2b52 original
robert
parents:
diff changeset
19
45ae2aeb2b52 original
robert
parents:
diff changeset
20 //if($browseNode->Name == "Literature & Fiction") //to return one level up the tree as well
45ae2aeb2b52 original
robert
parents:
diff changeset
21 // return true;
45ae2aeb2b52 original
robert
parents:
diff changeset
22 }
45ae2aeb2b52 original
robert
parents:
diff changeset
23 }
45ae2aeb2b52 original
robert
parents:
diff changeset
24 return false;
45ae2aeb2b52 original
robert
parents:
diff changeset
25 }
45ae2aeb2b52 original
robert
parents:
diff changeset
26 }
45ae2aeb2b52 original
robert
parents:
diff changeset
27
43
dbc006408d2b ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents: 38
diff changeset
28 global $out,$isbn,$Aserver,$Aassociates_id;
2
45ae2aeb2b52 original
robert
parents:
diff changeset
29 if(isset($_GET['isbn'])){
45ae2aeb2b52 original
robert
parents:
diff changeset
30 $isbn = $_GET['isbn'];
45ae2aeb2b52 original
robert
parents:
diff changeset
31 if(isset($_GET['locale'])){
45ae2aeb2b52 original
robert
parents:
diff changeset
32 $locale=$_GET['locale'];
45ae2aeb2b52 original
robert
parents:
diff changeset
33
45ae2aeb2b52 original
robert
parents:
diff changeset
34 switch ($locale) {
45ae2aeb2b52 original
robert
parents:
diff changeset
35 case "uk":
45ae2aeb2b52 original
robert
parents:
diff changeset
36 $loc = 1;
45ae2aeb2b52 original
robert
parents:
diff changeset
37 break;
45ae2aeb2b52 original
robert
parents:
diff changeset
38 case "us":
45ae2aeb2b52 original
robert
parents:
diff changeset
39 $loc = 0;
45ae2aeb2b52 original
robert
parents:
diff changeset
40 break;
45ae2aeb2b52 original
robert
parents:
diff changeset
41 case "ca":
45ae2aeb2b52 original
robert
parents:
diff changeset
42 $loc = 2;
45ae2aeb2b52 original
robert
parents:
diff changeset
43 break;
45ae2aeb2b52 original
robert
parents:
diff changeset
44 case "de":
45ae2aeb2b52 original
robert
parents:
diff changeset
45 $loc = 3;
45ae2aeb2b52 original
robert
parents:
diff changeset
46 break;
45ae2aeb2b52 original
robert
parents:
diff changeset
47 case "fr":
45ae2aeb2b52 original
robert
parents:
diff changeset
48 $loc = 4;
45ae2aeb2b52 original
robert
parents:
diff changeset
49 break;
45ae2aeb2b52 original
robert
parents:
diff changeset
50 case "":
45ae2aeb2b52 original
robert
parents:
diff changeset
51 $loc = 0;
45ae2aeb2b52 original
robert
parents:
diff changeset
52 break;
45ae2aeb2b52 original
robert
parents:
diff changeset
53 }
45ae2aeb2b52 original
robert
parents:
diff changeset
54 }
45ae2aeb2b52 original
robert
parents:
diff changeset
55 else{
45ae2aeb2b52 original
robert
parents:
diff changeset
56 $loc=0;
45ae2aeb2b52 original
robert
parents:
diff changeset
57 }
45ae2aeb2b52 original
robert
parents:
diff changeset
58 $output = "";
45ae2aeb2b52 original
robert
parents:
diff changeset
59
45ae2aeb2b52 original
robert
parents:
diff changeset
60 $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
45ae2aeb2b52 original
robert
parents:
diff changeset
61 $output .= "<BookDetails>";
45ae2aeb2b52 original
robert
parents:
diff changeset
62
45ae2aeb2b52 original
robert
parents:
diff changeset
63 $proceed = true;
49
a67bf725e87b put both paths in include_path and depend on that
Charlie Root
parents: 48
diff changeset
64 include "db.php";
2
45ae2aeb2b52 original
robert
parents:
diff changeset
65
45ae2aeb2b52 original
robert
parents:
diff changeset
66 $query = "CALL b_getBookInfo('$isbn', $loc)";
45ae2aeb2b52 original
robert
parents:
diff changeset
67 //select Timestamp,Title,Author,Binding,DeweyDecimal,ImageURL,SalesRank,PublicationDate,Publisher,Genre1,Genre2,Genre3 from books where ISBN = '$isbn'";
45ae2aeb2b52 original
robert
parents:
diff changeset
68 //echo $query;
46
b7376319d25a centralise db query even more
Charlie Root
parents: 44
diff changeset
69 $res = my_query($query,'gbd1');
2
45ae2aeb2b52 original
robert
parents:
diff changeset
70
45ae2aeb2b52 original
robert
parents:
diff changeset
71 if ( mysqli_num_rows( $res ) > 0 )
45ae2aeb2b52 original
robert
parents:
diff changeset
72 {
45ae2aeb2b52 original
robert
parents:
diff changeset
73 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC);
23
d606320ec331 post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents: 21
diff changeset
74 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
2
45ae2aeb2b52 original
robert
parents:
diff changeset
75 $timestamp=$rows['Timestamp'];
45ae2aeb2b52 original
robert
parents:
diff changeset
76 //echo $timestamp;
45ae2aeb2b52 original
robert
parents:
diff changeset
77 date_default_timezone_set('UTC');
45ae2aeb2b52 original
robert
parents:
diff changeset
78 $dbStamp = strtotime($timestamp);
45ae2aeb2b52 original
robert
parents:
diff changeset
79 //echo $dbStamp;
45ae2aeb2b52 original
robert
parents:
diff changeset
80 $unixTime = time();
45ae2aeb2b52 original
robert
parents:
diff changeset
81 //echo $unixTime;
45ae2aeb2b52 original
robert
parents:
diff changeset
82 $timeDiff = $unixTime - $dbStamp;
45ae2aeb2b52 original
robert
parents:
diff changeset
83 //echo $timeDiff;
45ae2aeb2b52 original
robert
parents:
diff changeset
84
45ae2aeb2b52 original
robert
parents:
diff changeset
85 if($timeDiff < 2592000) //if it has been updated in the last 30 days
45ae2aeb2b52 original
robert
parents:
diff changeset
86 {
45ae2aeb2b52 original
robert
parents:
diff changeset
87 $proceed = false;
45ae2aeb2b52 original
robert
parents:
diff changeset
88 $output .= "<ISBN>" . $isbn . "</ISBN>";
45ae2aeb2b52 original
robert
parents:
diff changeset
89 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>";
45ae2aeb2b52 original
robert
parents:
diff changeset
90 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>";
45ae2aeb2b52 original
robert
parents:
diff changeset
91 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>";
45ae2aeb2b52 original
robert
parents:
diff changeset
92 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>";
45ae2aeb2b52 original
robert
parents:
diff changeset
93 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>";
45ae2aeb2b52 original
robert
parents:
diff changeset
94 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>";
45ae2aeb2b52 original
robert
parents:
diff changeset
95 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>";
45ae2aeb2b52 original
robert
parents:
diff changeset
96 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>";
45ae2aeb2b52 original
robert
parents:
diff changeset
97 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>";
45ae2aeb2b52 original
robert
parents:
diff changeset
98 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>";
45ae2aeb2b52 original
robert
parents:
diff changeset
99 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>";
45ae2aeb2b52 original
robert
parents:
diff changeset
100 $output .= "<ProductGroup>Book</ProductGroup>";
45ae2aeb2b52 original
robert
parents:
diff changeset
101 }
45ae2aeb2b52 original
robert
parents:
diff changeset
102 }
23
d606320ec331 post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents: 21
diff changeset
103 else {
d606320ec331 post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents: 21
diff changeset
104 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
d606320ec331 post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents: 21
diff changeset
105 }
53
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
106 dl("gbd: ".strlen($output)." $proceed\n$output\n");
2
45ae2aeb2b52 original
robert
parents:
diff changeset
107 if($proceed == true)
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
108 {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
109 include "aws_signed_request.php";
49
a67bf725e87b put both paths in include_path and depend on that
Charlie Root
parents: 48
diff changeset
110 include_once "xml/doAmazonRequest.php";
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
111
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
112 //book does not exist already so look up all the info from browse nodes....
2
45ae2aeb2b52 original
robert
parents:
diff changeset
113
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
114 $Adefault=array(
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
115 'language' =>'en', //what language to render the page in
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
116 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
117 'page' =>1, //first page to show (we are counting from 1 not 0)
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
118 'operation' =>'ItemLookup', //what to do? //ItemSearch
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
119 'searchparameter' =>'ItemId', //what kind of search?
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
120 'searchindex' => 'Books',
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
121 'searchparameterdata'=>$isbn, //what to search for?
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
122 'search' =>$isbn,
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
123 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true'
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
124 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
125 'show_url' =>false, //debug: show XML request url to be send to Amazon?
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
126 'show_xml' =>false, //debug: show incoming XML code from Amazon?
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
127 );
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
128 //change the debug options to true if you want to activate them or call the script with '?show_array=true' to see what actual information you're getting from Amazon and how little my standard script is actually showing of it
2
45ae2aeb2b52 original
robert
parents:
diff changeset
129
45ae2aeb2b52 original
robert
parents:
diff changeset
130
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
131 //for all parameters see if the user has overruled it or use the default
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
132 foreach ($Adefault as $i=>$d) {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
133 $$i=isset($_GET[$i])?$_GET[$i]:$d;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
134 }
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
135
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
136 $parameters=array(
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
137 'Operation' =>$operation ,
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
138 'Keywords' =>urlencode($search) ,
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
139 "$searchparameter"=>$searchparameterdata ,
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
140 'ItemPage' =>$page , //which page?
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
141 'AssociateTag' =>$Aassociates_id[$locale],
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
142 'ResponseGroup' =>'ItemAttributes,Images,SalesRank,BrowseNodes'
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
143 );
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
144
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
145 if ($searchindex!='Books') {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
146 $parameters['SearchIndex']=$searchindex;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
147 }
2
45ae2aeb2b52 original
robert
parents:
diff changeset
148
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
149 try {
53
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
150 $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,2,'d');
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
151 //use this xml to pull out the necessary information and save it
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
152 set_error_handler(function () {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
153 global $out,$isbn;
43
dbc006408d2b ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents: 38
diff changeset
154 dl("Caught one bd: $isbn\n");
dbc006408d2b ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents: 38
diff changeset
155 dl(print_r($out, TRUE)."\n");
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
156 } );
49
a67bf725e87b put both paths in include_path and depend on that
Charlie Root
parents: 48
diff changeset
157 include "db.php";
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
158
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
159 $title="";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
160 $author="";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
161 $binding="";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
162 $dewey="";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
163 $imageURL="";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
164 $salesRank="";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
165 $pubDate="";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
166 $publisher="";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
167
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
168 $title = $xml->Items->Item->ItemAttributes->Title;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
169 $author = $xml->Items->Item->ItemAttributes->Author;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
170 $binding = $xml->Items->Item->ItemAttributes->Binding;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
171 $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
172 if($dewey == "")
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
173 $dewey = "null";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
174 $imageURL = $xml->Items->Item->MediumImage->URL;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
175 $salesRank = $xml->Items->Item->SalesRank;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
176 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
177 $publisher = $xml->Items->Item->ItemAttributes->Publisher;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
178 restore_error_handler();
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
179 if ($publisher and strlen($publisher)>30) {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
180 $publisher=substr($publisher,0,30);
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
181 }
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
182 if ($author and strlen($author)>30) {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
183 $author=substr($author,0,30);
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
184 }
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
185 if ($title and strlen($title)>100) {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
186 $title=substr($title,0,100);
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
187 }
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
188 $publisher=mysqli_real_escape_string($link,$publisher);
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
189 $author=mysqli_real_escape_string($link,$author);
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
190 $title=mysqli_real_escape_string($link,$title);
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
191 if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";}
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
192 if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";}
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
193 if (strlen($pubDate)==0) {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
194 $pubDate="null";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
195 }
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
196 else {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
197 $pubDate="\"$pubDate\"";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
198 }
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
199 /*echo $title;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
200 echo $author;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
201 echo $binding;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
202 echo $dewey;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
203 echo $imageURL;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
204 echo $salesRank;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
205 echo $pubDate;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
206 echo $publisher;*/
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
207
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
208 $genreID = "";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
209 $genre = "";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
210 $genArr = array();
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
211 $g1 = "null";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
212 $g2 = "null";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
213 $g3 = "null";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
214 if(isset($xml->Items->Item->BrowseNodes->BrowseNode)){
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
215 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
216 //sexy recursive function
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
217 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre);
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
218
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
219 if($genre != "")
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
220 $genArr[strval($genreID)] = strval($genre);
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
221 //$genArr[$i] = array(strval($genreID) => strval($genre));
2
45ae2aeb2b52 original
robert
parents:
diff changeset
222
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
223 //echo $genre;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
224 //echo $genreID;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
225
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
226 $genre = "";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
227 $genreID = "";
21
46382face560 use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents: 18
diff changeset
228 }
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
229
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
230 $g1 = "null";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
231 $g2 = "null";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
232 $g3 = "null";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
233 $loop = 1;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
234
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
235 foreach ($genArr as $key => $value) {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
236 //echo "$key => $value";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
237 if ($key>2047) {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
238 //HST added
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
239 break;
9
232deb0b066a tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents: 6
diff changeset
240 }
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
241 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
242 //echo $queryG;
46
b7376319d25a centralise db query even more
Charlie Root
parents: 44
diff changeset
243 $resG = my_query($queryG,'gbd2');
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
244
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
245 switch ($loop) {
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
246 case 1:
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
247 $g1 = $key;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
248 break;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
249 case 2:
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
250 $g2 = $key;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
251 break;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
252 case 3:
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
253 $g3 = $key;
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
254 break;
9
232deb0b066a tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents: 6
diff changeset
255 }
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
256
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
257 $loop++;
9
232deb0b066a tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents: 6
diff changeset
258 }
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
259 }
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
260
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
261 if($salesRank == "")
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
262 $salesRank = "null";
2
45ae2aeb2b52 original
robert
parents:
diff changeset
263
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
264 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
49
a67bf725e87b put both paths in include_path and depend on that
Charlie Root
parents: 48
diff changeset
265 include "db.php";
2
45ae2aeb2b52 original
robert
parents:
diff changeset
266
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
267 $title = strtr($title, '"', "'");
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
268 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,\"$publisher\",$g1,$g2,$g3,$loc)";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
269 //echo $queryInsert;
2
45ae2aeb2b52 original
robert
parents:
diff changeset
270
46
b7376319d25a centralise db query even more
Charlie Root
parents: 44
diff changeset
271 $res = my_query($queryInsert,'gbd3');
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
272 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
2
45ae2aeb2b52 original
robert
parents:
diff changeset
273
45ae2aeb2b52 original
robert
parents:
diff changeset
274
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
275 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
276 $output .= "<Title>" . htmlspecialchars($title) . "</Title>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
277 $output .= "<Author>" . htmlspecialchars($author) . "</Author>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
278 $output .= "<Binding>" . htmlspecialchars($binding) . "</Binding>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
279 $output .= "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
280 $output .= "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
281 $output .= "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
282 $output .= "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
283 $output .= "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
284 $output .= "<Genre1>" . htmlspecialchars($g1) . "</Genre1>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
285 $output .= "<Genre2>" . htmlspecialchars($g2) . "</Genre2>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
286 $output .= "<Genre3>" . htmlspecialchars($g3) . "</Genre3>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
287 $output .= "<ProductGroup>Book</ProductGroup>";
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
288 }
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
289 catch (Exception $e) {
43
dbc006408d2b ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents: 38
diff changeset
290 dl("gBD: dAR failed:".$e->getMessage()."\n");
53
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
291 if (isset($rows)) {
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
292 dl("gBD: returning stale info from DB: $timeDiff\n");
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
293 /* HST hack, copied from above */
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
294 $output .= "<ISBN>" . $isbn . "</ISBN>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
295 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
296 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
297 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
298 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
299 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
300 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
301 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
302 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
303 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
304 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
305 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
306 $output .= "<ProductGroup>Book</ProductGroup>";
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
307 }
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
308 else {
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
309 # Causes looping behaviour from client :-(
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
310 dl("gBD: no DB info either, bailing\n");
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
311 return;
dd93cb4b77ad more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
Charlie Root
parents: 49
diff changeset
312 }
36
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
313 }
e570b199108b use doAmazonRequest
Charlie Root
parents: 34
diff changeset
314 }
2
45ae2aeb2b52 original
robert
parents:
diff changeset
315 $output .= "</BookDetails>";
45ae2aeb2b52 original
robert
parents:
diff changeset
316
45ae2aeb2b52 original
robert
parents:
diff changeset
317 echo $output;
45ae2aeb2b52 original
robert
parents:
diff changeset
318 }
45ae2aeb2b52 original
robert
parents:
diff changeset
319 ?>