NewYork Times Bestseller API access in PHP


Posted in: php | Save to del.icio.us | Twit This! 20 Feb 2009

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




Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts



Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]
  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]
  • Martin: How can you carry over your own variables into the URL? I am using a form to POST a couple of var [...]
  • nancy: thanks very much ! first tools [...]

  • Users Online

    • 14 Users Online
    • 13 Guests, 1 Bot