Fetching articles from the NY Times API

·
Analogous to yesterday's code snippet for the Guardian Open Platform, here's how to fetch articles (extracts, not the full text) from the New York Times Article Search API:
<?php
define('API_KEY', 'YOUR_API_KEY');
define('API_URL', 'http://api.nytimes.com/svc/search/v1');
$n = 10;
$i = 0;
do{
  $start = $i * $n;
  print "$start\n";
  
  $params = array(
    'api-key' => API_KEY,
    'query' => 'des_facet:[SCIENCE AND TECHNOLOGY]',
    'offset' => $i,
    'fields' => 'byline,body,date,title,url,des_facet',
    );
  
  $data = json_decode(file_get_contents(API_URL . '/article?' . http_build_query($params)));
  foreach ($data->results as $item)
    file_put_contents(sprintf('articles/%s.js', md5($item->url)), json_encode($item)); 
  
  sleep(1);
} while ($start total && ++$i 

Sadly it doesn't seem possible to get the categories assigned to an individual article - adding "facets=des_facet" only provides the sum of the categories across the 10 results. (see comment below).

The same Nature News story as before has these similar articles:

  1. OBSERVATORY; Galaxies Made Simple, or at Least Less Complicated
  2. THE YEAR IN IDEAS; Dark Energy
  3. Q&A
  4. Galaxies Twice as Bright as They Seem, Study Finds
  5. Stars Suggest a Quark Twist And a New Kind of Matter
  6. OBSERVATORY; Galactic Puzzle Solved? Threads Tie It Together
  7. VAST 'HOLE' IN SPACE APPEARS TO DEFY THEORIES
  8. ESSAY; Computing, 2016: What Won't Be Possible?
  9. THE NOVEMBER REPORT; Scientists In Waiting
  10. A Puzzle Finally Makes the 'Cosmic Figures' Fit

and the 50 most similar articles have these categories:

  1. SPACE [17]
  2. PHYSICS [14]
  3. RESEARCH [6]
  4. STARS AND GALAXIES [6]
  5. UNIVERSE [6]
  6. REVIEWS [5]
  7. ASTRONOMY AND ASTROPHYSICS [5]
  8. BOOKS AND LITERATURE [4]
  9. HUBBLE SPACE TELESCOPE [4]
  10. COMPUTERS AND THE INTERNET [4]
  11. EDUCATION AND SCHOOLS [3]
  12. SOCIAL CONDITIONS AND TRENDS [3]
  13. TELESCOPES AND OBSERVATORIES [3]
  14. UNITED STATES ECONOMY [2]
  15. BIOGRAPHICAL INFORMATION [2]
  16. GENETICS AND HEREDITY [2]
  17. THIRD WORLD AND DEVELOPING COUNTRIES [2]
  18. PHOTOGRAPHY [2]
  19. ACCIDENTS AND SAFETY [2]