maicol07/orcid-php-client

ORCID 网络服务库

0.3.3 2023-08-16 09:26 UTC

This package is auto-updated.

Last update: 2024-09-16 11:50:27 UTC


README

ORCID 上发送和读取工作流的 PHP 客户端

基于 TKouchoanou/orcid-php-client

此库最初是为了支持 ORCID OAuth2 认证流程。它还支持基本资料访问,但仍在进行中。根据开发者的需要或由其他感兴趣方请求/贡献的更多功能将陆续推出。

用法

OAuth2

3 腿 Oauth 授权

要完成 3 腿 oauth 流程,您必须先让用户重定向到 ORCID 授权页面。

use Orcid\Oauth;
use Orcid\ApiScopes;

// Set up the config for the ORCID API instance
$oauth = (new Oauth($clientId, $clientSecret))
    ->scopes(ApiScopes::AUTHENTICATE)
    ->state($state)
    ->redirectUri($redirectUri)
    ->showLogin(true);

// Create and follow the authorization URL
header("Location: " . $oauth->getAuthorizationUrl());

在 ORCID 文档中描述的大部分选项(有关自定义用户授权体验的选项,请参阅http://members.orcid.org/api/customize-oauth-login-screen)都封装在 OAuth 类中。

用户授权您的应用程序后,他们将被重定向回您的重定向 URI。从那里,您可以用授权码交换访问令牌。

use Orcid\Oauth;
use Orcid\ApiScopes;

if (!isset($_GET['code'])) {
	// User didn't authorize our app
	throw new Exception('Authorization failed');
}

// Set up the config for the ORCID API instance
$oauth = (new Oauth($clientId, $clientSecret))
    ->redirectUri($redirectUri);

// Authenticate the user
$oauth->authenticate($_GET['code']);

// Check for successful authentication
if ($oauth->isAuthenticated()) {
	$orcid = new Profile($oauth);

	// Get ORCID iD
	$id = $orcid->id();
}

此示例使用 ORCID 公共 API。还有一个成员 API,但其 OAuth 流程基本上是相同的。

资料

如上述示例中所暗示的,一旦通过 OAuth 成功认证,您就可以对其他公共/成员 API 进行后续请求。例如

use Orcid\Profile;
$orcid = new Profile($oauth);

// Get ORCID profile details
$id    = $orcid->id();
$email = $orcid->email();
$name  = $orcid->fullName();

目前,profile 类仅支持有限数量的辅助方法来直接访问资料数据中的元素。根据需要将扩展。可以通过调用 raw() 方法获取资料输出的原始 JSON 数据。

请注意,某些字段(如电子邮件)如果用户未提供该字段,则可能返回 null。

环境和 API 类型

ORCID 支持两个一般 API 端点。第一个是他们的公共 API,第二个是为注册 ORCID 成员(在此场景中,成员资格并不意味着您有一个 ORCID 账户)。公共 API 是默认使用的,目前支持库提供的所有功能。但是,您可以调用以下命令切换到成员 API

use Orcid\Oauth;
$oauth = (new Oauth())->membersApi(true);

如果您明确想使用公共 API,可以通过调用来实现

use Orcid\Oauth;
$oauth = (new Oauth())->membersApi(false);

ORCID 还支持一个用于测试的沙箱环境。要使用此环境(默认为生产环境),而不是调用以下命令

use Orcid\Oauth;
$oauth = (new Oauth())->sandbox(true);

此函数的对应函数,尽管不是必需的,是

use Orcid\Oauth;
$oauth = (new Oauth())->sandbox(false);

工作

工作是一个类,允许您在您的 ORCID 账户上创建出版物。必须通过设置器将文档的数据添加到 Work 实例中,以便发送到 ORCID。Work 提供创建 Orcid 接受格式的 XML 文档的方法。

use Orcid\Work\Work;
use Orcid\Work\WorkType;
use PrinsFrank\Standards\Language\ISO639_1_Alpha_2;
use Orcid\Work\CitationType;
use PrinsFrank\Standards\Country\ISO3166_1_Alpha_2;
use Orcid\Work\ContributorRole;
use Orcid\Work\ContributorSequence;
// creation of an Orcid work
$work = (new Work())
    ->title("Les stalagmites du réseau du trou Noir")
    ->translatedTitle('The stalagmites of the Black hole network')
    ->translatedTitleLanguageCode(ISO639_1_Alpha_2::English) // These three methods about the title can be shortened to the first one (3 params)
    ->type(WorkType::WORKING_PAPER)
    ->url("the work url")
    ->journalTitle("naturephysic")
    ->citation("The work citation....")//if you don't set citationType formatted-unspecified will be set
    ->citationType(CitationType::FORMATTED_UNSPECIFIED)
    ->shortDescription("the work description...") // the description must be less than 500 characters
    ->publicationDate(\Carbon\Carbon::createFromDate(1998, 09, 20)) // the first parameter year is required if you want to set date
    ->languageCode(ISO639_1_Alpha_2::French)
    ->country(ISO3166_1_Alpha_2::United_States_of_America_the)
    //add Authors with Author FullName and role, by default the role 'author' will be chosen your can also add the orcidID and the sequence of author
    ->addContributor("Benjamin Lans", ContributorRole::AUTHOR,"1111-OOOO-2543-3333", ContributorSequence::FIRST)
    ->addContributor("Richard Maire", ContributorRole::EDITOR)
    ->addContributor("Richard Ortega", ContributorRole::CO_INVENTOR)
    ->addContributor("Guillaume Devès", ContributorRole::CO_INVESTIGATOR,"OOOO-1111-2222-3333", ContributorSequence::ADDITIONAL)
    //add subtitle
    ->subtitle("subtitle three")
    // add External Ident the type , the value, the url, the relationship by default url will be empty and relationship will be self . idtype and idValue   are required
    ->addExternalId("doi", "10.1038/nphys1170", "https://www.nature.com/articles/nphys1170")
    ->addExternalId("uri", "00199711");

发送 ORCID 工作的最小配置是定义标题、文档类型并至少添加一个外部标识符。

use Orcid\Work\Work;
use Orcid\Work\WorkType;
 // minimum configuration to create an Orcid work
$work = (new Work())
    ->title("Les stalagmites du réseau du trou Noir")
    ->type(WorkType::WORKING_PAPER)
    ->addExternalId("doi", "10.1038/nphys1170");

如果您想编辑一项工作,则需要 put-code。

$putCode = 14563; 
$work->putcode($putCode); 

作品

作品是一个从 arrayIterator 继承的类。它是 ORCID 作品的列表,我们可以通过 append 方法添加类型为 Work 的实例

use Orcid\Work\Works;
$works = new Works([$work1, $work2, $work3]);
// Or via the append method:
$works->append($firstwork);
$works->append($secondwork);
$works->append($thirdwork);

我们可以通过 foreach 迭代工作

use Orcid\Work\Work;

foreach ($works as $work){
    assert($work instanceof Work);
    $work->addContributor("Authorfullname", \Orcid\Work\ContributorRole::AUTHOR, "Author orcid ID", \Orcid\Work\ContributorSequence::FIRST); 
}

OClient

Orcid 客户端使得可以与包含在 OAuth 对象中的认证元素进行通信的 Orcid 计数进行通信,该 OAuth 对象传递给 Oclient 构造函数。

use Orcid\Work\OClient;
// Check for successful authentication

if ($oauth->isAuthenticated())
{
    // creation of an orcid client
	$orcidClient = new OClient($oauth);
}

Oclient 的不同方法

发送:允许您发送一个或多个出版物。它需要一个工作类实例的数组、一个要发送多个出版物的作品实例,或一个要发送单个出版物的作品类实例作为参数。

use Orcid\Work\OClient;
// send one or several work(s)
/** @var Work|Works|Work[] $works  */
/** @var OClient $orcidClient  */
$orcidClient->send($works); 

更新:此方法允许您修改已发送给Orcid的Work。您只能修改存在于orcid账户中并具有putCode的作品。要修改,请务必设置putCode。

use Orcid\Work\OClient;
// update a Work

/** @var OClient $orcidClient  */
/** @var OClient $orcidClient  */

$orcidClient->update($work); 

删除:允许您删除一个工作。它需要一个在orcid上的工作的putCode作为参数。

use Orcid\Work\OClient;
// delete a Work
$putcode=14563;
/** @var OClient $orcidClient  */
$orcidClient->delete($putcode); 

读取摘要:允许您读取由$oauth表示的账户持有者的Orcid注册中所有作品。

use Orcid\Work\OClient;
/** @var OClient $orcidClient  */
// read Summary
$orcidClient->readSummary()

读取:允许您通过读取其参数为int或string类型的putCode或putCode数组来读取一个或多个记录。putCode必须是一个数值,由Orcid返回。

use Orcid\Work\OClient;
// read work(s)
/** @var int|string|int[]|string[] $putCode */
/** @var OClient $orcidClient  */
$orcidClient->read($putCode);

OResponse

它是Oclient方法返回的响应对象。它包含Orcid返回的响应信息。请求通过curl发出。

use Orcid\Work\OClient;

/** @var OClient $orcidClient  */
$OResponse= $orcidClient->readSummary();
$code=$OResponse->getErrorCode();
$header=$OResponse->response->getHeaders();
$body=$OResponse->response->getBody()->getContents();

如果请求出错,Orcid将返回数据,可以通过这些方法检索,如果没有错误,这些方法将返回null或空字符串。

/** @var \Orcid\Work\OResponse $OResponse  */
if ($OResponse->hasError()) {
    $errorCode = $OResponse->getErrorCode();
    $userMessage = $OResponse->user_message;
    $developerMessage = $OResponse->developer_message;
}

关于使用ReadSummary方法读取orcid账户中所有工作记录,Oresponse有一个方法,返回读取的Orcid记录列表。如果Oresponse不是ReadSummary方法调用的响应,则此方法返回null。

use Orcid\Work\Works;
/** @var \Orcid\Work\OResponse $OResponse  */

if ($OResponse->hasSuccess()) {
    /** @var Records $recordWorkList */
    $recordWorkList = $OResponse->getWorkRecordList(); 
}

此方法返回一个Records实例,它是Record实例的列表。

Records和Record

它是一个实例,其属性集合表示用户orcid账户中的Orcid作品。它具有与Work实例(用于向Orcid发送作品的类)的一些共同属性,以及来自Orcid的具体属性。

use Orcid\Work\Works;
use Orcid\Work\Work;
/** @var Works $works */

// Returns date of last modification of ORCID registrations
$grouplastModifiedDate = $works->lastModifiedDate(); 

// returns a complex associative array coming directly from Orcid and containing the information on the work read.
$group = $works->getOrcidWorks(); 

foreach ($works as $work){
    assert($work instanceof Work);
    $putCode= $work->putCode();
    $workSource = $work->source();
    $workPath = $work->path();
    $lastModifiedDate = $work->lastModifiedDate(); // returns date of last modification of this record work
    $title = $work->title();

    // returns an external identifier array of type ExternalId
    $externalIds = $work->externals();
}

ExternalId

代表一个外部标识符,包含四个属性 $ idType、$ idValue、$ idUrl、$ idRelationship。

use Orcid\Work\ExternalId;
use Orcid\Work\Work;

/** @var Work $record */

$externalIds= $record->externals();

foreach ($externalIds as $externalId){
    assert($externalId instanceof ExternalId);
    $idType = $externalId->type();
    $idValue = $externalId->value(); 
    $idUrl = $externalId->url(); 
    $idRelationship = $externalId->relationship();
}