hubzero / orcid-php
ORCID 网络服务库
Requires
- php: >=5.6.0
- ext-curl: *
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.*
- satooshi/php-coveralls: 1.0.*
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();
配置文件类目前仅支持直接从配置文件数据访问有限数量的辅助方法。根据需要,这将会扩展。配置文件输出的原始 JSON 数据可以通过调用 raw()
方法获取。
请注意,某些字段(如电子邮件)如果用户未提供该字段,可能会返回 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();