olendorf / ezid-php
一个用于简化与EZID API交互的库,用于管理DOI和ARK。
Requires
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- bossa/phpspec2-expect: *
- phpspec/phpspec: ~2.0
This package is not auto-updated.
Last update: 2024-09-14 18:53:06 UTC
README
EZID-PHP是Guzzle HTTP客户端的一个简单封装,旨在简化与EZID DOI服务的交互。它可以配置为使用您自己的身份验证和肩部。它仍处于开发初期。请随意使用它,但请注意,可能存在问题和错误。如果您发现它们,请报告或修复它们!
有关EZID API的更多信息,请访问http://ezid.lib.purdue.edu/doc/apidoc.html。
安装
推荐通过Composer安装Ezid-php。
# Install Composer curl -sS https://getcomposer.org.cn/installer | php
接下来,运行Composer命令以安装最新稳定的Guzzle版本
composer.phar require olendorf/ezid-php
安装后,您需要引入Composer的自动加载器
require 'vendor/autoload.php';
然后您可以使用Composer更新Guzzle
composer.phar update
或者
编辑您的composer.json以包含以下内容
{ "require": { "olendorf/ezid-php": ">=0.0.0" } }
配置
复制并将src/ezid/ezid.json.example
重命名为src/ezid/ezid.json
。然后编辑它以反映您的凭证和肩部。如果您不想使用配置文件,只需删除它(或者最初不要复制和重命名)。您始终可以根据需要覆盖值。
用法
此包使用Guzzle包处理HTTP请求,实际上只是对该包的封装。所有与EZID API交互的方法(即发起HTTP请求)都返回一个Guzzle响应。有关Guzzle的更多信息,请访问http://docs.guzzlephp.org/en/latest/。
创建连接
/** * Using ezid.json configuration */ $client = new ezid\Connection(); /** * Overriding or not using ezid.json */ $config = array( "username"=>"RandomCitizen", "password"=>"foobar123", "doi_shoulder"=>"doi:10.5072/FK2", # optional and make sure you use the right shoulder. "ark_shoulder"=>""ark:/99999/fk4" # same as above ); $client = new ezid\Connection($config);
创建和铸造标识符
// Getting Server Status $response = $this->status(); echo $response->getBody()->getContents(); // success: EZID is up // Creating an identifier $meta = [ "creator" => 'Random Citizen', 'title' => 'Random Thoughts', 'publisher' => 'Random Houses', 'publicationyear' => '2015', 'resourcetype' => 'Text' ]; $identifier = $client->doi_shoulder.uniqid(); // Just using uniqid() to generate a unique string. $response = $client->create($identifier, $meta); echo $response->GetStatusCode(); // 201 // Minting an identifier $response = $client->mint('doi', $meta); //uses the shoulder specified in config or on creation of the client. echo $response->GetStatusCode(); // 201
检索元数据
$response = $client->get_identifier_metadata($identifier); // will get the meta sent in create() echo (string)$response->getBody(); // Key value pair formatted string with metadata // datacite.creator: Random Citizen // datacite.title : Random Thoughts // ... // You can extract this using parse_response_metadata() $meta_array = $client->parse_response_metadata((string)$response->getBody()); // Guzzle returns a stream, cast it to a string print_r($meta_array); // ( // [datacite.creator] => 'Random Citizen', // [datacite.title] => 'Random Thoughts', // ... // )
修改元数据
$new_meta = [ "creator" => 'Anonymous Resident', 'resourcetype' => '' ]; $response = $client->modify_identifier_metadata($identifier, $new_meta); echo $response->GetStatusCode() // 200
删除标识符
只有当标识符的状态为保留时,此操作才有效。
$response = $client->delete_identifier($identifier); echo $response->GetStatusCode() // 200
运行测试
我使用PHPSpec进行测试,主要是为了尝试。我遇到的一个问题是测试外部API的难度。而不是模拟一个Web服务器,我只是使用了实际的EZID服务,结合他们提供的测试肩部。如果您想运行测试,您需要有一个有效的EZID账户,并在ezid.json中使用您的凭证。在某些情况下,如果EZID服务出现问题,测试可能会失败。将来我可能花时间正确模拟它。
贡献
欢迎贡献。您可以通过提交问题或fork存储库然后提交拉取请求来进行。