Jump to content

PHP Script to fetch values from Search API


pjanakir

Recommended Posts

I have a large text file delimited with a | symbol consisting of 3 field columns : ISBN (or Barcode number), Catalog Key and OCLC Number in the format -

Barcode | Catalog Key | OCLC #

 

The Catalog Key can be ignored and if there's no catalog key for a specific barcode, that means there is no OCLC# for that record.

 

Using the WorldCat API (URL : http://www.worldcat.org/affiliate/tools?atype=wcapi ) and WorldCat Search API URL : http://worldcat.org/devnet/wiki/SearchAPIDetails ), we need to write a script to fetch information for each of the library items identified in the attached text file.

 

These barcodes would be used to look up the corresponding OCLC number of the publication that the barcode represents. This OCLC number would then be used to interrogate Worldcat to determine the number of different libraries that have the publication.

 

The output would be the number of libraries in Canada, US and RoW (Rest of the World) that have the publication - as 3 separate numbers - in a tab or pipe delimited file with columns for

 

Barcode

# Canada (library count in Canada)

# USA (library count in USA)

# RoW (library count in the Rest of the World)

Publication year

 

If it's hard to get the three geographical counts, then just give me one number for all locations worldwide, that is:

 

Barcode

#

Publication year

 

If it's hard to get the publication year, then that can be left out too but getting it from OCLC would help as a confirmation of Quest data accuracy.

 

I don't need a list of libraries that have each publication, just the number of libraries. If Quest doesn't have an OCLC number, then the output should be the barcode with the other fields as null (empty), not zeroes. What I want is a CSV data file that I can use to manipulate with Excel along with the PHP script.

 

Attached is a sample PHP test script that I came up with.

 

File - worldcat_holdings_count.php

------------------------------------

 

<?php

 

$WorldCatAPIKey = 'p0rR7Cc19hXVPEH14NrLdsE9KMvUNMx3zRn8ql9zxQeCIIQxbby1X2433EfMFFMsOvKllzi2OBPGMrt3'; // My current WorldCat Search API Key

 

if( strlen($_REQUEST['OCLCNumber']) > 0 and strlen($_REQUEST['Location']) > 0) {

 

$searchURL = "http://www.worldcat.org/webservices/catalog/content/libraries/{$_REQUEST['OCLCNumber']}?location=" . urlencode($_REQUEST['Location']) ;

 

/** We likely don't need the 'startLibrary', 'maximumLibraries', or 'libType' parameters in the requests :

these are to limit the number of libraries from which we retrieve results, and we want to include all

libraries within the specified location.

$searchURL .= '&startLibrary='. $_REQUEST[startLibrary];

$searchURL .= '&maximumLibraries='. $_REQUEST[MaximumLibraries];

$searchURL .= 'libtype='. $_REQUEST[LibType];

*/

 

$searchURL .= '&wskey='. $WorldCatAPIKey;

 

$xml = simplexml_load_file($searchURL); //Converts the well-formed XML document in the given file to an object

 

// Calculate the library counts

 

$xml->registerXPathNamespace("marc", "http://www.loc.gov/MARC21/slim"); // Registers a namespace context for the next XPath query

 

// Go get the Library Locations in the search results

$xml->registerXPathNamespace("marc", "http://www.loc.gov/MARC21/slim");

$location_ids = "";

 

$locations = $xml->xpath("//marc:record/marc:datafield[@tag=020]/marc:subfield[@code=a]"); // TODO : Set correct Marc Datafield for location

foreach ((array)$locations as $location) {

if (strlen($location) > 1) {

if (strpos($location[0], " ") > 0) {

$location_ids = $location_ids . substr($location, 0, strpos($location, " "));

} else {

$location_ids = $location_ids = $location_ids . $location[0];

}

if ($location != end($locations)) {

$location_ids = $location_ids . ',';

}

}

}

 

foreach($xml->xpath('//marc:record') as $publication ) {

$publication['xmlns:marc'] = 'http://www.loc.gov/MARC21/slim';

$field = simplexml_load_string($publication->asXML());

$barcode = $field->xpath("marc:datafield[@tag=020]/marc:subfield[@code=a]"); // TODO : Set correct Marc Datafield for barcode

$location = $field->xpath("marc:datafield[@tag=260]/marc:subfield[@code=b]"); // // TODO : Set correct Marc Datafield for location

$sumloc = array_sum($location); // Calculate the sum of values in the location array

$publication_date = $field->xpath("marc:datafield[@tag=260]/marc:subfield[@code=c]");

$publication_year=date("Y", strtotime("$publication_date")); // Extracting the full numeric representation of the publication year (4 digits) from publication_date

if (strpos($location[0], " ") > 0) {

$location_1 = substr($location[0], 0, strpos($location[0], " "));

} else {

$location_1 = $location[0];

}

$oclcnumber = $field->xpath("marc:controlfield[@tag=001]");

}

}

?>

 

 

[attachment deleted by admin]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.