What could possibly be better for a writer's ego other than being read and being praised? Hmm...
So I wanted to have a page that shows the books I've written, together with their Amazon sales rank and the average customer rating and number of reviews. It's really easy. I took one example out of the PEAR book and slightly modified it.
Implementation at a glance
- Get an Amazon Web Services (AWS) subscription id
- Get a copy of PEAR::Services_Amazon package
- Make a request
- Display the response
When making a request, you need to say what type of request it is and what type of response you want. It's documented here. For the type of request, look under "API reference -> Operations" and for the response type, look under "API reference -> Response Groups"
The code
// include the PEAR package require_once 'Services/AmazonECS4.php'; // Your AWS subscription id $subscriptionId = '1WQDAES5PQ**********'; // create a new client by supplying // subscription id $amazon = new Services_AmazonECS4($subscriptionId); $amazon->setLocale('US'); // output options // what do you need returned? $options = array(); $options['ResponseGroup'] = 'SalesRank,ItemAttributes,Reviews'; // for which books // comma-delimited list of ISBNs $items = '1904811795,1904811914,1904811132'; // do the request $result = $amazon->ItemLookup($items, $options); // check for errors if (PEAR::isError($result)) { print "An error occured<br/>"; print $result->getMessage() . "<br/>"; exit(); } // some spaghetti to display HTML response echo '<ul>'; foreach ($result['Item'] as $book) { // loop the books // URL echo '<li><a href="'. $book['DetailPageURL'] .'">'; // book title echo $book['ItemAttributes']['Title'], '</a><br />'; // authors, comma-delimited echo implode(', ',$book['ItemAttributes']['Author']); // sales rank echo '<br />Sales rank: ', $book['SalesRank']; // average rating if (!empty($book['CustomerReviews'])) { echo '<br />Rating: '; echo $book['CustomerReviews']['AverageRating']; echo ', based on '; echo $book['CustomerReviews']['TotalReviews'], ' reviews'; } echo '</li>'; } echo '</ul>'; <a href="#" onclick="javascript:document.getElementById('response').style.display='block';" >Show complete response</a> <pre id="response" style="display: none"> print_r($result); </pre>
Comments? Find me on BlueSky, Mastodon, LinkedIn, Threads, Twitter