New York Times API (PHP)

The Mount Prospect Public Library wanted to have the New York Times Bestsellers list on their website. So I decided to create a program that would do that for them. The program works by fetching JSON data from the NYT API, comparing it against the libraries catalog to make sure they own a copy, then creating an xml file with information about the items in the list. A page on the website loads that XML file and parses it. The whole program is run on a cron job once a week when the new Best Sellers lists come out.

chdir(dirname(__FILE__));

function PullData($listType)
{
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 $query = array(
  // You'll need your own API key
  "api-key" => "",
  "list" => $listType
 );
 curl_setopt($curl, CURLOPT_URL,
  "https://api.nytimes.com/svc/books/v3/lists.json" . "?" . http_build_query($query)
 );

 $data = json_decode(curl_exec($curl));
 $listTitle = $data->results[0]->list_name;

 $listCount = count($data->results);
 $fileTitle = str_replace(' ', '_', $listTitle);
 $fileTitle1 = str_replace(':', '', $fileTitle);

 // Opening the file to write to it.
 $filename = $fileTitle1.".xml";
 $file = fopen($filename, "w");
 fwrite($file, "");
 fwrite($file, "\n");
 fwrite($file,"\n$listTitle");

 for ($i=0; $i < $listCount ; $i++) 
 { 
  $isbn = $data->results[$i]->book_details[0]->primary_isbn13;
  $title1 = $data->results[$i]->book_details[0]->title;
  $title = str_replace('&', '&', $title1);
  $author1 = $data->results[$i]->book_details[0]->author;
  $author = str_replace('&', '&', $author1);
  $description1 = $data->results[$i]->book_details[0]->description;
  $description = str_replace('&', '&', $description1);


  $cSession1 = curl_init();
  // You'll need your own version of the SirsiDynix Web Services API
  $sirsiDynixWebServicesApi = "";
  // You'll need your own clientID for the SirsiDynix Web Services API
  $clientID = "";
  curl_setopt($cSession1, CURLOPT_URL, $sirsiDynixWebServicesApi."/rest/standard/searchCatalog?clientID=".$clientID."&term=$isbn&indexID=ISBNEX");
  curl_setopt($cSession1, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($cSession1, CURLOPT_HEADER, false);
  $response = curl_exec($cSession1);
  curl_close($cSession1);

  $catalogCheck = simplexml_load_string($response);
	$inCatalog = $catalogCheck->totalHits;
	$bibNumber = $catalogCheck->titleInfo->titleKey; 
	if ($inCatalog == 1){
		fwrite($file, "\n");

		    if($isbn != ""){
			fwrite($file, "\nhttps://secure.syndetics.com/index.aspx?isbn=$isbn/mc.gif&upc=&oclc=&client=mounp");
			}
			
			else {
	        fwrite($file, "\nimages/missing-image.png");
			}
		 fwrite($file, "\n$bibNumber");	
		 fwrite($file, "\n$title");
		
		 if($author != "")
		{
		 fwrite($file, "\n$author");
		}

		if($description != "")
		{
		 fwrite($file, "\n$description");
		}
		 fwrite($file, "\nhttp://mtpros.ent.sirsi.net/client/default/search/results?qu=$bibNumber");
		 fwrite($file, "\n$isbn");
		 fwrite($file, "\n");
	}
	
 }
 fwrite($file, "\n");
 fclose($file);

} // End PullData


// Create a new XML file for each book list
PullData("combined-print-and-e-book-nonfiction");
PullData("combined-print-and-e-book-fiction");
PullData("hardcover-fiction");
PullData("young-adult");
PullData("childrens-middle-grade");
PullData("hardcover-nonfiction");

echo "Finished";


Posted

in

, ,

by