1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package API |
4
|
|
|
* @author Iurii Makukh <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh |
6
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A very basic example of client functions that can be used to request API provided by this module |
11
|
|
|
* Please adapt to your framework / environment! |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Log in and get an authorization token |
16
|
|
|
* @param string $url |
17
|
|
|
* @param string $client_id |
18
|
|
|
* @param string $client_secret |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
function login($url, $client_id, $client_secret) |
22
|
|
|
{ |
23
|
|
|
$ch = curl_init($url); |
24
|
|
|
|
25
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
26
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, array('client_id' => $client_id, 'client_secret' => $client_secret)); |
27
|
|
|
|
28
|
|
|
$result = curl_exec($ch); |
29
|
|
|
curl_close($ch); |
30
|
|
|
|
31
|
|
|
$decoded = json_decode($result, true); |
32
|
|
|
return is_array($decoded) ? $decoded : $result; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Request API with the authorization token |
37
|
|
|
* @param string $url |
38
|
|
|
* @param string $access_token |
39
|
|
|
* @param array $data |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
function request($url, $access_token, array $data = array()) |
43
|
|
|
{ |
44
|
|
|
$ch = curl_init($url); |
45
|
|
|
|
46
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
47
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
48
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $access_token")); |
49
|
|
|
|
50
|
|
|
$result = curl_exec($ch); |
51
|
|
|
curl_close($ch); |
52
|
|
|
|
53
|
|
|
$decoded = json_decode($result, true); |
54
|
|
|
return is_array($decoded) ? $decoded : $result; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Log in and fetch API data |
59
|
|
|
* @param string $url |
60
|
|
|
* @param string $client_id |
61
|
|
|
* @param string $client_secret |
62
|
|
|
* @param array $post_data |
63
|
|
|
* @param bool $force_login |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
function fetch($url, $client_id, $client_secret, array $post_data = array(), $force_login = false) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if (session_status() !== PHP_SESSION_ACTIVE) { |
69
|
|
|
session_start(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$now = time(); |
73
|
|
|
|
74
|
|
|
if (!$force_login && isset($_SESSION['api_access_token']) && $now < $_SESSION['api_token_expires']) { |
75
|
|
|
return request($url, $_SESSION['api_access_token'], $post_data); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$result = login($url, $client_id, $client_secret); |
79
|
|
|
|
80
|
|
|
if (isset($result['access_token']) && isset($result['expires_in'])) { |
81
|
|
|
|
82
|
|
|
$_SESSION['api_access_token'] = $result['access_token']; |
83
|
|
|
$_SESSION['api_token_expires'] = $now + $result['expires_in']; |
84
|
|
|
|
85
|
|
|
return request($url, $result['access_token'], $post_data); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $result; |
89
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: