comparison php/read/getBookDetails.php @ 2:45ae2aeb2b52

original
author robert
date Wed, 28 Dec 2016 12:44:57 +0000
parents
children a5e5475bf357
comparison
equal deleted inserted replaced
1:7a532f1e0f07 2:45ae2aeb2b52
1 <?php
2
3 function findGenre($browseNode, &$ID, &$gen)
4 {
5 if($browseNode->Name == "Subjects")
6 {
7 return true;
8 }
9 else
10 {
11 if($browseNode->Ancestors->BrowseNode)
12 {
13 if(findGenre($browseNode->Ancestors->BrowseNode, $ID, $gen) == true)
14 {
15 $gen = $browseNode->Name;
16 $ID = $browseNode->BrowseNodeId;
17
18 //if($browseNode->Name == "Literature & Fiction") //to return one level up the tree as well
19 // return true;
20 }
21 }
22 return false;
23 }
24 }
25
26 if(isset($_GET['isbn'])){
27 $isbn = $_GET['isbn'];
28 if(isset($_GET['locale'])){
29 $locale=$_GET['locale'];
30
31 switch ($locale) {
32 case "uk":
33 $loc = 1;
34 break;
35 case "us":
36 $loc = 0;
37 break;
38 case "ca":
39 $loc = 2;
40 break;
41 case "de":
42 $loc = 3;
43 break;
44 case "fr":
45 $loc = 4;
46 break;
47 case "":
48 $loc = 0;
49 break;
50 }
51 }
52 else{
53 $loc=0;
54 }
55 $output = "";
56
57 $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
58 $output .= "<BookDetails>";
59
60 $proceed = true;
61 include "../../../private/db.php";
62
63 $query = "CALL b_getBookInfo('$isbn', $loc)";
64 //select Timestamp,Title,Author,Binding,DeweyDecimal,ImageURL,SalesRank,PublicationDate,Publisher,Genre1,Genre2,Genre3 from books where ISBN = '$isbn'";
65 //echo $query;
66 $res = mysqli_query($link, $query) or exit( mysqli_error( $link ));
67
68 if ( mysqli_num_rows( $res ) > 0 )
69 {
70 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC);
71 $timestamp=$rows['Timestamp'];
72 //echo $timestamp;
73 date_default_timezone_set('UTC');
74 $dbStamp = strtotime($timestamp);
75 //echo $dbStamp;
76 $unixTime = time();
77 //echo $unixTime;
78 $timeDiff = $unixTime - $dbStamp;
79 //echo $timeDiff;
80
81 if($timeDiff < 2592000) //if it has been updated in the last 30 days
82 {
83 $proceed = false;
84 $output .= "<ISBN>" . $isbn . "</ISBN>";
85 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>";
86 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>";
87 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>";
88 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>";
89 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>";
90 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>";
91 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>";
92 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>";
93 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>";
94 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>";
95 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>";
96 $output .= "<ProductGroup>Book</ProductGroup>";
97 }
98 }
99 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
100 include "../../../private/db.php";
101
102 if($proceed == true)
103 {
104 include "aws_signed_request.php";
105 //book does not exist already so look up all the info from browse nodes....
106
107 $Adefault=array(
108 'language' =>'en', //what language to render the page in
109 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us
110 'page' =>1, //first page to show (we are counting from 1 not 0)
111 'operation' =>'ItemLookup', //what to do? //ItemSearch
112 'searchparameter' =>'ItemId', //what kind of search?
113 'searchindex' => 'Books',
114 'searchparameterdata'=>$isbn, //what to search for?
115 'search' =>$isbn,
116 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true'
117 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending
118 'show_url' =>false, //debug: show XML request url to be send to Amazon?
119 'show_xml' =>false, //debug: show incoming XML code from Amazon?
120 );
121 //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
122
123 $Aassociates_id=array(
124 'uk' => 'bookwhack-21',
125 'us' => 'your02b-20',
126 'ca' => 'book009-20',
127 'de' => 'book04c-21',
128 'fr' => 'book07f-21',
129 );
130
131 $Aserver=array(
132 'ca' => array(
133 'ext' => 'ca' , //Canadian normal server
134 'nor' => 'http://www.amazon.ca' , //Canadian normal server
135 'xml' => 'http://xml.amazon.com' , //Canadian xml server
136 ),
137 'de' => array(
138 'ext' => 'de' , //German normal server
139 'nor' => 'http://www.amazon.de' , //German normal server
140 'xml' => 'http://xml-eu.amazon.com', //German xml server
141 ),
142 'fr' => array(
143 'ext' => 'fr' , //French normal server
144 'nor' => 'http://www.amazon.fr' , //French normal server
145 'xml' => 'http://xml-eu.amazon.com', //French xml server
146 ),
147 'jp' => array(
148 'ext' => 'jp' , //Japanese normal server, not co.jp!
149 'nor' => 'http://www.amazon.co.jp' , //Japanese normal server
150 'xml' => 'http://xml.amazon.com' , //Japanese xml server
151 ),
152 'uk' => array(
153 'ext' => 'co.uk' , //UK normal server
154 'nor' => 'http://www.amazon.co.uk' , //UK normal server
155 'xml' => 'http://xml-eu.amazon.com', //UK xml server
156 ),
157 'us' => array(
158 'ext' => 'com' , //USA normal server
159 'nor' => 'http://www.amazon.com' , //USA normal server
160 'xml' => 'http://xml.amazon.com' , //USA xml server
161 ),
162 );
163
164 $public_key ="AKIAJBXEHTNCU6LLFNIA";
165 $private_key="Dgyv7aR6uGe3OtY95Dj6hHpDS/UEtyboMWpJchYA";
166
167 //for all parameters see if the user has overruled it or use the default
168 foreach ($Adefault as $i=>$d) {
169 $$i=isset($_GET[$i])?$_GET[$i]:$d;
170 }
171
172 $parameters=array(
173 'Operation' =>$operation ,
174 'Keywords' =>urlencode($search) ,
175 'SearchIndex' =>$searchindex , //Books for example.
176 "$searchparameter"=>$searchparameterdata ,
177 'ItemPage' =>$page , //which page?
178 'AssociateTag' =>$Aassociates_id[$locale],
179 'ResponseGroup' =>'ItemAttributes,Images,SalesRank,BrowseNodes'
180 );
181
182
183 $ext=$Aserver[$locale]['ext'];
184 $file_data=$ext;
185 ksort($parameters);
186 foreach ($parameters as $i=>$d) {
187 $file_data.='&'.$i.'='.$d;
188 }
189
190 $url=aws_signed_request($ext,$parameters,$public_key,$private_key);
191 //echo $url;
192
193 $crl = curl_init();
194 $timeout = 5;
195 curl_setopt ($crl, CURLOPT_URL,$url);
196 curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
197 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
198 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
199 $out = curl_exec($crl);
200 curl_close($crl);
201
202 //echo $out;
203
204 $xml = new SimpleXMLElement($out);
205 //use this xml to pull out the necessary information and save it
206
207 $title="";
208 $author="";
209 $binding="";
210 $dewey="";
211 $imageURL="";
212 $salesRank="";
213 $pubDate="";
214 $publisher="";
215
216 $title = $xml->Items->Item->ItemAttributes->Title;
217 $author = $xml->Items->Item->ItemAttributes->Author;
218 $binding = $xml->Items->Item->ItemAttributes->Binding;
219 $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber;
220 if($dewey == "")
221 $dewey = "null";
222 $imageURL = $xml->Items->Item->MediumImage->URL;
223 $salesRank = $xml->Items->Item->SalesRank;
224 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate;
225 $publisher = $xml->Items->Item->ItemAttributes->Publisher;
226
227 /*echo $title;
228 echo $author;
229 echo $binding;
230 echo $dewey;
231 echo $imageURL;
232 echo $salesRank;
233 echo $pubDate;
234 echo $publisher;*/
235
236 $genreID = "";
237 $genre = "";
238 $genArr = array();
239 $g1 = "null";
240 $g2 = "null";
241 $g3 = "null";
242 if(isset($xml->Items->Item->BrowseNodes->BrowseNode)){
243 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){
244 //sexy recursive function
245 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre);
246
247 if($genre != "")
248 $genArr[strval($genreID)] = strval($genre);
249 //$genArr[$i] = array(strval($genreID) => strval($genre));
250
251 //echo $genre;
252 //echo $genreID;
253
254 $genre = "";
255 $genreID = "";
256 }
257
258 $g1 = "null";
259 $g2 = "null";
260 $g3 = "null";
261 $loop = 1;
262
263 foreach ($genArr as $key => $value) {
264 //echo "$key => $value";
265 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table
266 //echo $queryG;
267 $resG = mysqli_query($link, $queryG);
268
269 switch ($loop) {
270 case 1:
271 $g1 = $key;
272 break;
273 case 2:
274 $g2 = $key;
275 break;
276 case 3:
277 $g3 = $key;
278 break;
279 }
280
281 $loop++;
282 }
283 }
284
285 if($salesRank == "")
286 $salesRank = "null";
287
288 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
289 include "../../../private/db.php";
290
291 $title = strtr($title, '"', "'");
292 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,\"$pubDate\",\"$publisher\",$g1,$g2,$g3,$loc)";
293 //echo $queryInsert;
294
295 $resG = mysqli_query($link, $queryInsert) or exit( mysqli_error( $link ));
296 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
297
298
299 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>";
300 $output .= "<Title>" . htmlspecialchars($title) . "</Title>";
301 $output .= "<Author>" . htmlspecialchars($author) . "</Author>";
302 $output .= "<Binding>" . htmlspecialchars($binding) . "</Binding>";
303 $output .= "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>";
304 $output .= "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>";
305 $output .= "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>";
306 $output .= "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>";
307 $output .= "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>";
308 $output .= "<Genre1>" . htmlspecialchars($g1) . "</Genre1>";
309 $output .= "<Genre2>" . htmlspecialchars($g2) . "</Genre2>";
310 $output .= "<Genre3>" . htmlspecialchars($g3) . "</Genre3>";
311 $output .= "<ProductGroup>Book</ProductGroup>";
312 }
313
314 $output .= "</BookDetails>";
315
316 echo $output;
317 }
318 ?>