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:

  Copyright (c) 2009 The New York Times Company.  All Rights Reserved.
      Paperback Nonfiction
          A former climber builds schools in villages in Pakistan and Afghanistan.
          by Greg Mortenson and David Oliver Relin
          Greg Mortenson and David Oliver Relin

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

2 thoughts to “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 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 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.