[Web4lib] Amazon image function

Cloutman, David DCloutman at co.marin.ca.us
Tue Jan 29 17:09:16 EST 2008


I see two possible problems with your approach.

1. It doesn't adequately test for failure. If you got text for some sort
of error for instance, it seems like the test for an empty string might
fail.
2. It makes a query to the Amazon server every time, so if there is a
problem with the remote resource, it fails.

I would recommend the following.

1. Instead of using file_get_contents(), use the Curl library. This will
allow you to look at your response headers, and not display anything
other than a 200.
2. Some sort of caching seems in order. You could store images in a
database for a given query, and associate them with an expiration time.
That way you don't overload the server, and there wouldn't be an issue
for time outs with images that had been recently downloaded. Instead you
could first query your local database of cached images, and pass that
back to your user. Then you don't have to worry about time outs. If
there is no cached image, or it's expired, then you could try the more
risky process of an ad hoc query against a third party server, which may
or may not time out. Caching would also speed up your response time.

I hope that helps. These are just quick suggestions from skimming your
code, so I could be off track.

- David



---
David Cloutman <dcloutman at co.marin.ca.us>
Electronic Services Librarian
Marin County Free Library 

-----Original Message-----
From: web4lib-bounces at webjunction.org
[mailto:web4lib-bounces at webjunction.org] On Behalf Of Glen Wiley
Sent: Tuesday, January 29, 2008 1:39 PM
To: web4lib at webjunction.org
Subject: [Web4lib] Amazon image function


Hi everyone,

I know people have discussed Amazon book images issues on this listserv 
before, but here is a more technical question.

I'm using this php code to pull Amazon book cover images into a new book

list.  It does work, but I wondered if anyone had a suggestion about how

to do it better.  We aren't getting very many successful image displays 
because many of them time out or error out.  So out of 100 new books, I 
get 10 images appear.  Any suggestions here?

function RESTQuery($isbn){
   //1VE157GS2Z7SMVS61G02
   $base         = 'http://ecs.amazonaws.com/onca/xml';
   $query_string = '';
   
   $params = array(
       'ItemId' => "$isbn",
       'IdType' => "ASIN",
       'Service' => 'AWSECommerceService',
       'Operation' => 'ItemLookup',
       #'SearchIndex' => 'Books',
       #'sort'  => '+relevance',
       'ResponseGroup'  => 'Images',
       'AWSAccessKeyId' => '1VE157GS2Z7SMVS61G02',
       'Style' =>    "XML",
       'XMLEscaping' => "Double",
       'Version'    => "2007-07-16"
   );
   
   foreach ($params as $key => $value) {
       $query_string .= "$key=" . urlencode($value) . "&";
   }
   
   $url = "$base?$query_string";  
   
   $xmlstr = file_get_contents($url);
   if($xmlstr == "") return;
   $xml = new SimpleXMLElement($xmlstr);
   #return $xml->asXml();
   $imageURL = $xml->Items->Item->MediumImage->URL;
   
   if($imageURL != ""){
       return "<a href=\"http://www.amazon.com\"><img src=\"$imageURL\" 
/></a>";    
   }
   else{
       return "";
   }
}


--Glen

 

------------------------------------------------------------------------

Glen Wiley
Metadata Librarian

Metadata Services <http://metadata.library.cornell.edu/>

107E Olin Library
Cornell University
Ithaca, NY 14853/
/gsw55 at cornell.edu <mailto:gsw55 at cornell.edu>
607-254-5145
 

--/ Our Vision is to be internationally recognized and respected as a 
center of metadata expertise. --/

 

_______________________________________________
Web4lib mailing list
Web4lib at webjunction.org
http://lists.webjunction.org/web4lib/

Email Disclaimer: http://www.co.marin.ca.us/nav/misc/EmailDisclaimer.cfm



More information about the Web4lib mailing list