PHP code example of survos / nara-php-api

1. Go to this page and download the library: Download survos/nara-php-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

survos / nara-php-api example snippets


use Survos\Nara\ClientFactory;

$client = ClientFactory::create($yourApiKey);

// Search records (raw array response)
$results = $client->search(['q' => 'constitution']);
echo $results['body']['hits']['total']['value'];

// Search with typed DTOs
$results = $client->searchWithDtos(['q' => 'constitution']);
foreach ($results as $result) {
    echo $result->record->title;
    echo $result->record->naId;
}

// Search transcriptions
$transcriptions = $client->transcriptionsSearchWithDtos(['q' => 'constitution']);
foreach ($transcriptions as $t) {
    echo $t->contribution;
}

// Get single record by NA ID
$record = $client->getRecordByNaId(1667751);
echo $record->title;

use Survos\Nara\ClientFactory;

// Writes need your API key *and* your Catalog user UUID (the contribution
// is attributed to that account). Set it once on the client...
$client = ClientFactory::create($yourApiKey, userId: $yourCatalogUuid);

// Submit the full text of a digital object as a transcription. A transcription
// attaches to one digital object (a single page/image), so you pass both the
// record's naId AND that object's id. Re-submitting overwrites the object's
// transcription (NARA keeps history).
$result = $client->submitTranscription(
    targetNaId: 170821856,
    transcription: $ocrText,
    targetObjectId: 170821859,
);

if ($result->success) {
    echo "Saved as contribution {$result->contributionId}";
} else {
    // 401/422 etc. are returned (not thrown) so a bulk loop can log & continue.
    echo "Failed (HTTP {$result->statusCode}): {$result->message}";
}

// Tags and comments attach at the record level:
$client->submitTag(521451, 'president');
$client->submitComment(521451, 'This photo also appears in series ...');

// Or pass the user UUID per call instead of on the client:
$client->submitTranscription(170821856, $ocrText, 170821859, userId: $someOtherUuid);

foreach ($client->getDigitalObjects(170821856) as $object) {
    // $object is a Survos\Nara\Model\DigitalObject
    $text = $ocrByObjectId[$object->objectId] ?? null;
    if ($text !== null) {
        $client->submitTranscription(170821856, $text, $object->objectId);
    }
}

$mine = $client->contributionsSearch(['userName' => 'yourCatalogUsername']);
// read the `userId` off any returned contribution
bash
composer 
bash
# Using environment variable
export NARA_API_KEY="your-key"
php bin/search.php search "constitution"

# Using --api-key option
php bin/search.php search "constitution" --api-key=your-key
php bin/search.php search "presidents" --limit=5

# Lookup by NA ID
php bin/search.php search 1667751

# Verbose output
php bin/search.php search "constitution" -vvv --limit=1