[Web4lib] Amazon image function

Casey Bisson cbisson at plymouth.edu
Tue Jan 29 17:19:45 EST 2008


Glen,

You might consider using curl instead of file_get_contents(). It's a  
little more code, but you'll be able to see the entire response and  
get more info about what may have failed in the connection.

http://www.php.net/curl

Here's some code I'm using to connect to the flickr API (it's at hand  
and I'm embarrassed of the AWS code I wrote):

	@$c = curl_init();
	if( $c ){
		curl_setopt( $c, CURLOPT_URL, 'http://api.flickr.com/services/ 
rest/' );
		curl_setopt( $c, CURLOPT_POST, 1 );
		curl_setopt( $c, CURLOPT_POSTFIELDS, http_build_query( $params ));
		curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt( $c, CURLOPT_CONNECTTIMEOUT, 10 );
		$r = curl_exec($c);
	}else // no curl, try fopen...
		$r = file_get_contents( 'http://api.flickr.com/services/rest/?' .  
http_build_query( $params ));

	if( !$r )
		die( 'Darn. It no worky. Perhaps flickr is down? Perhaps this host  
does not have curl or fopen wrappers?' );


(the code is written to fallback to file_get_contents if curl isn't  
available.)

Swap the inputs, run $r through SimpleXML, and see what happens. It  
may be that you'll have to play with the CURLOPT_CONNECTTIMEOUT value.

--Casey



Casey Bisson
__________________________________________

Information Architect
Plymouth State University
Plymouth, New Hampshire
http://oz.plymouth.edu/~cbisson/
ph: 603-535-2256


On Jan 29, 2008, at 4:39 PM, Glen Wiley wrote:

> 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/



More information about the Web4lib mailing list