-
Notifications
You must be signed in to change notification settings - Fork 0
/
orcid.php
51 lines (44 loc) · 1.65 KB
/
orcid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php require_once('connect.php');
class orcid_api {
public string $client_id;
private string $secret;
public string $auth_url;
public string $token_url = "https://orcid.org/oauth/token";
public string $data_url;
public string $orcid;
private string $access_token;
function __construct() {
foreach (orcidvars() as $key => $var) $this->{$key} = $var;
$this->auth_url = "https://orcid.org/oauth/authorize?client_id=".$this->client_id."&response_type=code&scope=/authenticate";
}
function auth_url(string $redirect): string { return "{$this->auth_url}&redirect_uri={$redirect}"; }
function get_auth_token(string $code) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->token_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['code' => $code, 'client_id' => $this->client_id, 'client_secret' => $this->secret, 'grant_type' => 'authorization_code']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if (!isset($response->error)) {
$this->orcid = $response->orcid;
$this->data_url = "https://pub.orcid.org/v3.0/{$this->orcid}/record";
$this->access_token = $response->access_token;
}
return $response;
}
function get_record() {
if (!$this->orcid) return false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->data_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer {$this->access_token}",
"Content-Type: application/orcid+json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
}
}