scriptotek/sru-client

用于生成搜索/检索URL请求并解析响应的软件包

v0.7.2 2021-12-20 07:01 UTC

This package is auto-updated.

Last update: 2024-09-11 22:54:56 UTC


README

Coverage Code Quality Latest Stable Version Total Downloads

php-sru-client

一个简单的PHP软件包,用于生成通过URL搜索/检索(SRU)请求,并返回QuietSimpleXMLElement对象。响应对象是一个迭代器,以便轻松迭代结果,抽象出多次请求的过程。

如果您更喜欢简单的文本响应,可以看看php-sru-search软件包。

使用Composer安装

使用Composer安装sru-client,并使用Guzzle等HTTP库

composer require scriptotek/sru-client php-http/guzzle6-adapter http-interop/http-factory-guzzle

我们使用HTTP发现来发现HTTP客户端HTTP工厂实现,因此Guzzle可以与任何其他PSR-17/PSR-18兼容的库进行交换。

配置客户端

require_once('vendor/autoload.php');
use Scriptotek\Sru\Client as SruClient;

$sru = new SruClient('http://bibsys-network.alma.exlibrisgroup.com/view/sru/47BIBSYS_NETWORK', [
    'schema' => 'marcxml',
    'version' => '1.2',
    'user-agent' => 'MyTool/0.1',
]);

搜索和检索

获取与给定CQL查询匹配的所有记录

$records = $sru->all('alma.title="Hello world"');
foreach ($records as $record) {
	echo "Got record " . $record->position . " of " . $records->numberOfRecords() . "\n";
	// processRecord($record->data);
}

其中 $recordRecord 实例,而 $record->dataQuietSimpleXMLElement 实例。

all() 方法会在内部为您处理续传;Records 生成器会继续获取记录,直到结果集耗尽。默认批次大小为10,但您可以将服务器支持的所有数字作为第二个参数传递给 all() 方法。

如果您查询某些标识符,可以使用方便的方法 first()

$record = $sru->first('alma.isbn="0415919118"');

结果是 Record 对象,如果没有找到则为 null

使用explain获取有关服务器的信息

$urls = array(
    'http://sru.bibsys.no/search/biblio',
    'http://lx2.loc.gov:210/LCDB',
    'http://services.d-nb.de/sru/zdb',
    'http://api.libris.kb.se/sru/libris',
);

foreach ($urls as $url) {

    $sru = new SruClient($url, [
        'version' => '1.1',
        'user-agent' => 'MyTool/0.1'
    ]);

    try {
        $response = $sru->explain();
    } catch (\Scriptotek\Sru\Exceptions\SruErrorException $e) {
        print 'ERROR: ' . $e->getMessage() . "\n";
        continue;
    }

    printf("Host: %s:%d\n", $response->host, $response->port);
    printf("  Database: %s\n", $response->database->identifier);
    printf("  %s\n", $response->database->title);
    printf("  %s\n", $response->database->description);
    print "  Indexes:\n";
    foreach ($response->indexes as $idx) {
        printf("   - %s: %s\n", $idx->title, implode(' / ', $idx->maps));
    }

}

Laravel 5集成

将服务提供者添加到config/app.php中的'providers'数组

Scriptotek\Sru\Providers\SruServiceProvider::class,

可选地,将外观添加到同一文件的'aliases'数组中

'SruClient'      => Scriptotek\Sru\Facades\SruClient::class,

创建配置文件config/sru.php

$ php artisan vendor:publish --provider="Scriptotek\Sru\Providers\SruServiceProvider"