III item# to bib# conversion

Here is a great way to quickly convert III item numbers to bib numbers. You could stick this function in your php script and go to town:

(PHP 4 >= 4.3.0, PHP 5 with fopen_wrappers)

[php]
$opac_server = “my.opac.org”;

function get_bibnum($itemnum, $opac_server) {
$item_url = ‘http://’ . $opac_server . ‘/record=i’ . $itemnum;
$item_rec = file_get_contents($item_url);
if (preg_match(‘%record=b(.+?)”>%s’, $item_rec, $bnum_matches)) {
$bibnum = $bnum_matches[1];
return $bibnum;
} else {
return 0;
}
}
?>
[/php]

It’s really so simple, I can’t believe I didn’t think of it before. I’m actually using it now to cache the results in a database so I’m not always chewing on the OPAC to get the result.

Enjoy!

[update]

In response to your comment/question Ryan, this is verbatim the code I’m using to cache the bib numbers. First, this is the table I’m using:

[mysql]
DESCRIBE item2bib;
+———+————+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+———+————+——+—–+———+—————-+
| id | int(8) | NO | PRI | NULL | auto_increment |
| itemnum | varchar(9) | NO | MUL | | |
| bibnum | varchar(9) | NO | | | |
+———+————+——+—–+———+—————-+
3 rows in set (0.00 sec)
[/mysql]

This is the code (which is actually inside a class, so you’ll need to strip the class-specific stuff, but you get the general idea):

[php]
public function get_bibnum($itemnum) {
$db = DB::connect($this->dsn);
$bibnum = $db->getone(“SELECT bibnum FROM item2bib WHERE itemnum = ‘$itemnum’ LIMIT 1″);
if ($bibnum) { return $bibnum; }
$item_url = ‘http://’ . $this->opac_server . ‘/record=i’ . $itemnum;
$item_rec = file_get_contents($item_url);
if (preg_match(‘%record=b(.+?)”>< %s', $item_rec, $bnum_matches)) { $bibnum = $bnum_matches[1]; $db->query(“INSERT INTO item2bib VALUES (0, ‘$itemnum’, ‘$bibnum’)”);
return $bibnum;
} else {
return 0;
}
}
?>
[/php]

Run some data through and verify that it’s working correctly:

[mysql]
mysql> select count(distinct itemnum) as itemnum, count(distinct bibnum) as bibnum, count(*) as total from item2bib;
+———+——–+——-+
| itemnum | bibnum | total |
+———+——–+——-+
| 45809 | 33303 | 45809 |
+———+——–+——-+
1 row in set (0.00 sec)
[/mysql]

[/update]


About this entry