$vol = '10';
                     //echo "{$vol}: {$row['creator']}\n";
                 } elseif ($year >= 364 && $year <= 395) {
                     $nomismaId['volume'] = 'ric.9';
                     $vol = '9';
                     //echo "{$vol}: {$row['creator']}\n";
                 } elseif (strpos($row['creator'], 'Constantius II') !== FALSE || strpos($row['creator'], 'Decentius') !== FALSE || strpos($row['creator'], 'Constans') !== FALSE || strpos($row['creator'], 'Constantine II') !== FALSE || strpos($row['creator'], 'Julianus') !== FALSE || strpos($row['creator'], 'Jovian') !== FALSE || strpos($row['creator'], 'Magnentius') !== FALSE) {
                     $nomismaId['volume'] = 'ric.8';
                     $vol = '8';
                 }
             }
             $page = $pieces[0];
             $num = $pieces[1];
             //only evaluate if the page is an integer
             if (isset($vol) && is_numeric($page)) {
                 $authority = get_authority($vol, (int) $page);
                 if (isset($authority)) {
                     $nomismaId['auth'] = $authority;
                     //ignore uncertain attribution
                     if (substr($num, -1) != '?') {
                         $cointype = query_ocre($nomismaId, $num);
                         if (isset($cointype)) {
                             echo "Matched {$cointype}\n";
                             generate_record($row, $cointype);
                         }
                     }
                 }
             }
         }
     }
 }
function parse_page($page, $apiKey)
{
    global $records;
    echo "Page {$page}\n";
    $query = '(standardreferencenumber:RRC+OR+standardreferencenumber:RIC+OR+standardreferencenumber:Price)';
    //$query = '"RIC+IX,+p.+235,+88b(1)"';
    $service = 'http://api.harvardartmuseums.org/object?size=100&classification=Coins&q=' . $query . '+AND+department:"Department%20of%20Ancient%20and%20Byzantine%20Art%20%26%20Numismatics"&apikey=' . $apiKey . '&page=' . $page;
    $json = file_get_contents($service);
    $data = json_decode($json);
    foreach ($data->records as $record) {
        $references = explode(';', $record->standardreferencenumber);
        foreach ($references as $reference) {
            if (preg_match('/RRC/', $reference)) {
                $num = explode(' ', $reference);
                $row = array();
                //generate record metadata array
                $row = construct_metadata($record);
                $row['reference'] = $reference;
                //ignore uncertain coins
                if (substr($num[1], -1) != '?') {
                    $cointype = 'http://numismatics.org/crro/id/rrc-' . str_replace('/', '.', $num[1]);
                    $file_headers = @get_headers($cointype);
                    if ($file_headers[0] == 'HTTP/1.1 200 OK') {
                        echo "{$row['objectnumber']}: {$cointype}\n";
                        $row['cointype'] = $cointype;
                    }
                }
                $records[] = $row;
                //end RRC processing
            } elseif (preg_match('/Price/', $reference)) {
                $num = explode(' ', $reference);
                $row = array();
                //generate record metadata array
                $row = construct_metadata($record);
                $row['reference'] = $reference;
                //ignore uncertain coins
                if (substr($num[1], -1) != '?') {
                    $cointype = 'http://numismatics.org/pella/id/price.' . $num[1];
                    $file_headers = @get_headers($cointype);
                    if ($file_headers[0] == 'HTTP/1.1 200 OK') {
                        echo "{$row['objectnumber']}: {$cointype}\n";
                        $row['cointype'] = $cointype;
                    }
                }
                $records[] = $row;
                //end PELLA processing
            } elseif (preg_match('/RIC/', $reference)) {
                $originalReference = $reference;
                $pieces = explode(',', $reference);
                $volNum = $pieces[0];
                //1. get volume number and then strip from reference. 2. trim leading or trailing commas. 3. trim spaces
                switch ($volNum) {
                    case 'RIC I 2':
                        $parsedVolume = $volNum;
                        $volume = 'ric.1(2)';
                        break;
                    case 'RIC II':
                        $parsedVolume = $volNum;
                        $volume = 'ric.2';
                        break;
                    case 'RIC II.1 2':
                    case 'RIC II (2)':
                        $parsedVolume = $volNum;
                        $volume = 'ric.2_1(2)';
                        break;
                    case 'RIC III':
                        $parsedVolume = $volNum;
                        $volume = 'ric.3';
                        break;
                    case 'RIC IV(B)':
                    case 'RIC IV(C)':
                    case 'RIC IV':
                    case 'RIC iv/1':
                    case 'RIC iv/2':
                    case 'RIC iv/3':
                        $parsedVolume = $volNum;
                        $volume = 'ric.4';
                        break;
                    case 'RIC V':
                    case 'RIC V(1)':
                    case 'RIC V(2)':
                    case 'RIC v/1':
                    case 'RIC v/2':
                        $parsedVolume = $volNum;
                        $volume = 'ric.5';
                        break;
                    case 'RIC VI':
                        $parsedVolume = $volNum;
                        $volume = 'ric.6';
                        break;
                    case 'RIC VII':
                        $parsedVolume = $volNum;
                        $volume = 'ric.7';
                        break;
                    case 'RIC VIII':
                        $parsedVolume = $volNum;
                        $volume = 'ric.8';
                        break;
                    case 'RIC IX':
                        $parsedVolume = $volNum;
                        $volume = 'ric.9';
                        break;
                    case 'RIC X':
                        $parsedVolume = $volNum;
                        $volume = 'ric.10';
                        break;
                    default:
                        $volume = null;
                }
                //echo "{$volume}\n";
                echo "{$originalReference}\n";
                //strip parsed Volume and trailing/leading commas and whitespace
                $reference = trim(trim(str_replace($parsedVolume, '', $reference), ','));
                //strip whitespace following the p.
                $reference = str_replace('p. ', 'p.', $reference);
                //only process currently published volumes
                if ($volume != null && strlen($reference) > 0) {
                    $row = array();
                    //generate record metadata array
                    $row = construct_metadata($record);
                    $row['reference'] = $originalReference;
                    //parse remaining numbers of the reference, determine page number based on 'p.'
                    $arr = explode(' ', $reference);
                    $pageNumber = '';
                    $id = '';
                    foreach ($arr as $item) {
                        if (strpos($item, 'p.') !== FALSE) {
                            $pageNumber = trim(trim(str_replace('p.', '', $item), ','));
                        } else {
                            $id = trim(trim($item, ','));
                        }
                    }
                    if (strlen($id) > 0 && strlen($pageNumber) > 0) {
                        //ignore uncertain coins
                        if (substr($id, -1) != '?') {
                            $authority = get_authority($parsedVolume, $pageNumber);
                            if ($authority != null) {
                                //if $authority is properly resolved, try OCRE lookups
                                $prefix = "{$volume}.{$authority}.";
                                if (preg_match('/[a-zA-Z]/', $id)) {
                                    //if it is volume 9, attempt to parse subtype numbers
                                    if ($volume == 'ric.9' && strpos($id, '(') !== FALSE) {
                                        //strip space
                                        $id = str_replace(' ', '', $id);
                                        preg_match('/([a-z0-9]+)\\(([0-9a-z])\\)/', $id, $matches);
                                        //first try to match on the subtype URI
                                        if (isset($matches[1]) and isset($matches[2])) {
                                            $cointype = "http://numismatics.org/ocre/id/{$prefix}" . strtoupper($matches[1]) . '.' . $matches[2];
                                            $file_headers = @get_headers($cointype);
                                            if ($file_headers[0] == 'HTTP/1.1 200 OK') {
                                                echo "{$row['objectnumber']}: {$cointype}\n";
                                                $row['cointype'] = $cointype;
                                            } else {
                                                //if it can't find the subtype, just try to match on parent type
                                                $cointype = "http://numismatics.org/ocre/id/{$prefix}" . strtoupper($matches[1]);
                                                $file_headers = @get_headers($cointype);
                                                if ($file_headers[0] == 'HTTP/1.1 200 OK') {
                                                    echo "{$row['objectnumber']}: {$cointype}\n";
                                                    $row['cointype'] = $cointype;
                                                }
                                            }
                                        }
                                    } else {
                                        //try uppercase first
                                        $upper = strtoupper($id);
                                        $cointype = "http://numismatics.org/ocre/id/{$prefix}" . urlencode($upper);
                                        $file_headers = @get_headers($cointype);
                                        if ($file_headers[0] == 'HTTP/1.1 200 OK') {
                                            echo "{$row['objectnumber']}: {$cointype}\n";
                                            $row['cointype'] = $cointype;
                                        } else {
                                            //then try default
                                            $cointype = "http://numismatics.org/ocre/id/{$prefix}" . urlencode($id);
                                            $file_headers = @get_headers($cointype);
                                            if ($file_headers[0] == 'HTTP/1.1 200 OK') {
                                                echo "{$row['objectnumber']}: {$cointype}\n";
                                                $row['cointype'] = $cointype;
                                            }
                                        }
                                    }
                                } else {
                                    $cointype = "http://numismatics.org/ocre/id/{$prefix}" . urlencode($id);
                                    $file_headers = @get_headers($cointype);
                                    if ($file_headers[0] == 'HTTP/1.1 200 OK') {
                                        echo "{$row['objectnumber']}: {$cointype}\n";
                                        $row['cointype'] = $cointype;
                                    }
                                }
                            }
                        }
                    }
                    $records[] = $row;
                }
                //end RIC processing
            }
        }
    }
    //if the page isn't the last, then move forward to the next one.
    if ($page < $data->info->pages) {
        $page++;
        parse_page($page, $apiKey);
    }
}