comparison xml/getAmazonInfo.php @ 5:55445b456ad0

short-circuit error response handling, add debugging
author robert
date Wed, 28 Dec 2016 13:07:32 +0000
parents a6b55ae70617
children 1dfe64e365a0
comparison
equal deleted inserted replaced
4:a6b55ae70617 5:55445b456ad0
1 <?php 1 <?php
2 2
3 $lastReqTime=0;
3 function getAmazonDet($isbn,$go,$localeIn) 4 function getAmazonDet($isbn,$go,$localeIn)
4 { 5 {
5 global $output; 6 global $output;
6 $Adefault=array( 7 $Adefault=array(
7 'language' =>'en', //what language to render the page in 8 'language' =>'en', //what language to render the page in
91 $file_data.='&'.$i.'='.$d; 92 $file_data.='&'.$i.'='.$d;
92 } 93 }
93 94
94 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); 95 $url=aws_signed_request($ext,$parameters,$public_key,$private_key);
95 //echo $url; 96 //echo $url;
96 file_put_contents('/var/ywww/debug/phpDebug',"parms: ". 97 file_put_contents('/var/ywww/debug/phpDebug',"parms: $go".
97 print_r($parameters,TRUE)."\n",FILE_APPEND); 98 print_r($parameters,TRUE)."\n",FILE_APPEND);
98 $crl = curl_init(); 99 $crl = curl_init();
99 $timeout = 5; 100 $timeout = 5;
100 curl_setopt ($crl, CURLOPT_URL,$url); 101 curl_setopt ($crl, CURLOPT_URL,$url);
101 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 102 curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
102 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 103 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
103 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 104 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
105 /* $timeDiff=((int)(microtime(true)*1000))-$lastReqTime;
106 if ($timeDiff<1000) {
107 usleep((1000-$timeDiff)*1000); // microseconds
108 }*/
104 $output = curl_exec($crl); 109 $output = curl_exec($crl);
110 //$lastReqTime=(int)(microtime(true)*1000); // milliseconds
105 curl_close($crl); 111 curl_close($crl);
106 112
107 /*$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><ItemLookupErrorResponse xmlns=\"http://ecs.amazonaws.com/doc/2009-03-31/\"><Error><Code>AccountLimitExceeded</Code><Message>Account limit of 2056 requests per hour exceeded.</Message></Error><RequestID>290ed059-730c-4789-93b4-6d21e11053d3</RequestID></ItemLookupErrorResponse>");*/ 113 /*$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><ItemLookupErrorResponse xmlns=\"http://ecs.amazonaws.com/doc/2009-03-31/\"><Error><Code>AccountLimitExceeded</Code><Message>Account limit of 2056 requests per hour exceeded.</Message></Error><RequestID>290ed059-730c-4789-93b4-6d21e11053d3</RequestID></ItemLookupErrorResponse>");*/
108 $xml = new SimpleXMLElement($output);
109 $review = ""; 114 $review = "";
110 set_error_handler(function () { 115 // HST added this
111 global $output; 116 if (preg_match("<Error>",$output)) {
112 file_put_contents('/var/ywww/debug/phpDebug', "Caught one: ",FILE_APPEND); 117 $xml = new SimpleXMLElement($output);
113 file_put_contents('/var/ywww/debug/phpDebug', 118 $resName=$xml->getName();
114 print_r($output, TRUE)."\n", 119 $code=$xml->Error->Code;
115 FILE_APPEND); 120 file_put_contents('/var/ywww/debug/phpDebug',"Losing: $resName, ".
121 $xml->Error->Code."\n",FILE_APPEND);
122 }
123 else {
124 $xml = new SimpleXMLElement($output);
125 set_error_handler(function () {
126 global $output;
127 file_put_contents('/var/ywww/debug/phpDebug',
128 "Caught one?: ".$searchparameterdata,
129 FILE_APPEND);
130 file_put_contents('/var/ywww/debug/phpDebug',
131 print_r($output, TRUE)."\n",
132 FILE_APPEND);
116 } ); 133 } );
117 $review = $xml->Items->Item->CustomerReviews->IFrameURL; 134 $review = $xml->Items->Item->CustomerReviews->IFrameURL;
118 // The above is failing repeatedly -- 135 // The above is failing repeatedly --
119 //PHP Notice: Trying to get property of non-object in 136 //PHP Notice: Trying to get property of non-object in
120 // /var/ywww/xml/getAmazonInfo.php on line [109] 137 // /var/ywww/xml/getAmazonInfo.php on line [109]
121 // See the dumped structure at the end of this file for the 138 // See the dumped structure at the end of this file for the
122 // cause 139 // cause
123 restore_error_handler(); 140 restore_error_handler();
124 //echo $review; 141 //echo $review;
125 $review1 = ""; 142 $review1 = "";
126 $review2 = ""; 143 $review2 = "";
127 $review3 = ""; 144 $review3 = "";
145 }
128 if ($review != "") 146 if ($review != "")
129 { 147 {
130 $text = @file_get_contents($review . "&truncate=300"); 148 $text = @file_get_contents($review . "&truncate=300");
131 $removeTop1 = preg_replace('~<div class="crIFrameHeaderLeftColumn">(.*?)<div class="crIFrameHeaderHistogram">~si', '', $text); 149 $removeTop1 = preg_replace('~<div class="crIFrameHeaderLeftColumn">(.*?)<div class="crIFrameHeaderHistogram">~si', '', $text);
132 $removeTop2 = preg_replace('~<div class="crIFrameHeaderHistogram">(.*?)<div class="crIframeReviewList">~si', '', $text); 150 $removeTop2 = preg_replace('~<div class="crIFrameHeaderHistogram">(.*?)<div class="crIframeReviewList">~si', '', $text);
233 $errorCode = $xml->Error->Code; 251 $errorCode = $xml->Error->Code;
234 //echo $errorCode; 252 //echo $errorCode;
235 253
236 if($errorCode != "AccountLimitExceeded") 254 if($errorCode != "AccountLimitExceeded")
237 { 255 {
238 if($go == 1) 256 if($go == 1) {
257 file_put_contents('/var/ywww/debug/phpDebug',"win: ".
258 $xml->Items->Item->ItemAttributes->Title."\n",
259 FILE_APPEND);
239 return $output; 260 return $output;
261 }
240 else 262 else
241 { 263 {
242 $title = $xml->Items->Item->ItemAttributes->Title; 264 $title = $xml->Items->Item->ItemAttributes->Title;
243 $author = $xml->Items->Item->ItemAttributes->Author; 265 $author = $xml->Items->Item->ItemAttributes->Author;
244 $binding = $xml->Items->Item->ItemAttributes->Binding; 266 $binding = $xml->Items->Item->ItemAttributes->Binding;