NewYork Times Bestseller API access in PHP

The New York Times has started offering a set of APIs for their various data services. On January 27th they released the API for their Best-Seller list. According to NYTimes :

Like many organizations, we hope to encourage innovation through collaboration. When you build applications, create mashups and otherwise reveal the potential of our data, we learn more about what our readers want and gain insight into how news and information can be reimagined. We’re hoping you’ll show us what’s next for The Times.

Currently the list offers every weekly list since June 2008 which will eventually go back to the 1930s. According to their blog, the API offers details about specific best sellers, including historical rank information and links to New York Times reviews and excerpts. I created a short class to access the api using CURL. To access the api you need to sign up for an API key here. The api returns data in xml, json or sphp (serialized php).

A sample code using the class is shown below:

$books = new nyTimesBooks("your api key here");
$books = $books->format("xml") // Set return format to xml
                ->listDate("2008-10-01") // publication date (YYYY-MM-DD)
                ->listType("Series-Books"); // List type
 
echo $books->get_bestsellers();

The list type can be any of the following:

* Hardcover-Fiction
* Hardcover-Nonfiction
* Hardcover-Advice
* Paperback-Nonfiction
* Paperback-Advice
* Trade-Fiction-Paperback
* Picture-Books
* Chapter-Books
* Paperback-Books
* Series-Books
* Mass-Market-Paperback

Or you can get the list as follows:

$books = new nyTimesBooks("your api key here");
echo $books->get_list_names();

You can also use the class without any options which will return the current list in xml format with the list type of ‘Mass-Market-Paperback’.

$books = new nyTimesBooks("your api key here");
echo $books->get_bestsellers();

A sample xml output is shown below:

<?xml version="1.0"?>
<result_set>
  <status>OK</status>
  <copyright>Copyright (c) 2009 The New York Times Company.  All Rights Reserved.</copyright>
  <num_results>20</num_results>
  <results>
    <book>
      <rank>1</rank>
      <list_name>Paperback Nonfiction</list_name>
      <bestsellers_date>2008-09-21</bestsellers_date>
      <published_date>2008-10-05</published_date>
      <weeks_on_list>86</weeks_on_list>
      <rank_last_week>0</rank_last_week>
      <asterisk>0</asterisk>
      <dagger>0</dagger>
      <book_details>
        <book_detail>
          <title>THREE CUPS OF TEA</title>
          <description>A former climber builds schools in villages in Pakistan and Afghanistan.</description>
          <contributor>by Greg Mortenson and David Oliver Relin</contributor>
          <author>Greg Mortenson and David Oliver Relin</author>
          <contributor_note/>
          <price>15</price>
          <age_group/>
          <publisher>Penguin</publisher>
        </book_detail>
      </book_details>
      <isbns>
        <isbn>
          <isbn13>9780143038252</isbn13>
          <isbn10>0143038257</isbn10>
        </isbn>
        <isbn>
          <isbn13>9780670034826</isbn13>
          <isbn10>0670034827</isbn10>
        </isbn>
        <isbn>
          <isbn13>9781597226240</isbn13>
          <isbn10>159
.
.
.

The code still needs to implement the following requests:
1. search best-seller lists
2. history of a best seller

api


2 thoughts on “NewYork Times Bestseller API access in PHP

  1. I’m working on using this in a public-library-related Drupal module that will link users from the NYTimes data (and also from Amazon.com RSS feeds) to records of those items within the library catalog. Would there be any problem with me distributing a modified version of this code on Drupal.org at some point as part of my module? It’s not ready yet, but it could be within the next month or so. The file would retain the attribution information at the top with your name and website.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>