PHP Script to fetch values from Search API

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 and WorldCat Search API, 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.

So where is your code?

edit: why attach a .txt file? just paste the code here.

It’s in the attachment I have given. It’s attached to the thread.

<?php

class foo {
public $name;
}

$con = mysql_connect(“localhost”,“root”,“”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}
else
{
mysql_select_db(“worldcat”, $con);
// echo ‘Connected’;
$result = mysql_query(“SELECT OCLCNumber FROM worldcat limit 1”);
//$obj = mysql_fetch_object($result, ‘foo’);
echo “<table border=‘1’>
<tr>
<th>BarCode</th>
<th>CatalogKey</th>
<th>OCLCNumber</th>
<th>LibraryCounts</th>
<th>PublicationYear</th>
</tr>”;

  //echo 'ISBN' . "--&gt; " . 'CatalogKey'. "--&gt;" .'OCLCNumber'. "--&gt;" .'LibraryCounts';
  //		echo "&lt;br /&gt;";
  while($row = mysql_fetch_array($result))
  {
	echo "&lt;tr&gt;";
	echo "&lt;td&gt;" . $row['BarCode'] . "&lt;/td&gt;";
	echo "&lt;td&gt;" . $row['CatalogKey'] . "&lt;/td&gt;";
	echo "&lt;td&gt;" . $row['OCLCNumber'] . "&lt;/td&gt;";
	echo "&lt;td&gt;" . $row['LibraryCounts'] . "&lt;/td&gt;";
	echo "&lt;td&gt;" . $row['PublicationYear'] . "&lt;/td&gt;";

	echo "&lt;/tr&gt;";

  }

}

$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-&gt;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) &gt; 1) {
		if (strpos($location[0], " ") &gt; 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-&gt;xpath('//marc:record') as $publication ) {
	$publication['xmlns:marc'] = 'http://www.loc.gov/MARC21/slim';
	$field = simplexml_load_string($publication-&gt;asXML());
	$barcode = $field-&gt;xpath("marc:datafield[@tag='020']/marc:subfield[@code='a']"); // TODO : Set correct Marc Datafield for barcode
	$location = $field-&gt;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-&gt;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], " ") &gt; 0) {
		$location_1 = substr($location[0], 0, strpos($location[0], " "));
	} else {
		$location_1 = $location[0];
	} 
	$oclcnumber = $field-&gt;xpath("marc:controlfield[@tag='001']");
}

}
?>