alexandrump / orcid-php
ORCID 网络服务的库
Requires
- php: >=5.6.0
- ext-curl: *
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.*
- satooshi/php-coveralls: 1.0.*
This package is auto-updated.
Last update: 2024-09-07 14:20:59 UTC
README
ORCID 的 PHP 库
这个库是为了支持 ORCID OAuth2 认证流程而开始的。它还支持基本资料访问,但仍在进行中。随着开发者的需求或由其他感兴趣的相关方请求/贡献,将会有更多功能添加。
使用方法
OAuth2
三脚 Oauth 授权
要完成三脚 Oauth 流程,你必须首先将用户重定向到 ORCID 授权页面。
// Set up the config for the ORCID API instance $oauth = new Oauth; $oauth->setClientId($clientId) ->setScope('/authenticate') ->setState($state) ->showLogin() ->setRedirectUri($redirectUri); // Create and follow the authorization URL header("Location: " . $oauth->getAuthorizationUrl());
在 ORCID 文档中描述的大多数选项(有关自定义用户授权体验的http://members.orcid.org/api/customize-oauth-login-screen)都被封装在 OAuth 类中。
用户授权您的应用后,他们将被重定向回您的重定向 URI。从那里,您可以将授权代码交换为访问令牌。
if (!isset($_GET['code'])) { // User didn't authorize our app throw new Exception('Authorization failed'); } $oauth = new Oauth; $oauth->setClientId($clientId) ->setClientSecret($clientSecret) ->setRedirectUri($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 进行后续请求。例如
$orcid = new Profile($oauth); // Get ORCID profile details $id = $orcid->id(); $email = $orcid->email(); $name = $orcid->fullName();
目前,资料类仅支持有限数量的辅助方法来直接访问资料数据中的元素。这将在需要时扩展。通过调用 raw()
方法可以获得资料输出的原始 JSON 数据。
请注意,某些字段(如电子邮件)如果用户没有提供该字段,可能会返回 null。
环境和 API 类型
ORCID 支持两个通用的 API 端点。第一个是他们的公共 API,第二个是为注册的 ORCID 成员(在这个场景中,会员资格不仅仅意味着您有一个 ORCID 账户)。默认情况下使用公共 API,并且目前支持库提供的所有功能。但是,您可以通过调用
$oauth = new Oauth; $oauth->useMembersApi();
如果您明确想使用公共 API,可以通过调用
$oauth = new Oauth; $oauth->usePublicApi();
ORCID 还支持一个用于测试的沙盒环境。要使用此环境,而不是默认的生产环境,您可以调用以下命令
$oauth = new Oauth; $oauth->useSandboxEnvironment();
此函数的对应物,尽管不是必需的,是
$oauth = new Oauth; $oauth->useProductionEnvironment();