These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | return json_decode($result, true); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Request API with the authorization token |
||
36 | * @param string $url |
||
37 | * @param string $access_token |
||
38 | * @param array $data |
||
39 | * @return mixed |
||
40 | */ |
||
41 | function request($url, $access_token, array $data = array()) |
||
42 | { |
||
43 | $ch = curl_init($url); |
||
44 | |||
45 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
46 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
||
47 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $access_token")); |
||
48 | |||
49 | $result = curl_exec($ch); |
||
50 | curl_close($ch); |
||
51 | |||
52 | return $result; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Log in and fetch API data |
||
57 | * @param string $url |
||
58 | * @param string $client_id |
||
59 | * @param string $client_secret |
||
60 | * @param array $post_data |
||
61 | * @param bool $force_login |
||
62 | * @return mixed |
||
63 | */ |
||
64 | function fetch($url, $client_id, $client_secret, array $post_data = array(), $force_login = false) |
||
0 ignored issues
–
show
|
|||
65 | { |
||
66 | if (session_status() !== PHP_SESSION_ACTIVE) { |
||
67 | session_start(); |
||
68 | } |
||
69 | |||
70 | $now = time(); |
||
71 | |||
72 | if (!$force_login && isset($_SESSION['api_access_token']) && $now < $_SESSION['api_token_expires']) { |
||
73 | return request($url, $_SESSION['api_access_token'], $post_data); |
||
74 | } |
||
75 | |||
76 | $result = login($url, $client_id, $client_secret); |
||
77 | |||
78 | if (isset($result['access_token']) && isset($result['expires_in'])) { |
||
79 | |||
80 | $_SESSION['api_access_token'] = $result['access_token']; |
||
81 | $_SESSION['api_token_expires'] = $now + $result['expires_in']; |
||
82 | |||
83 | return request($url, $result['access_token'], $post_data); |
||
84 | } |
||
85 | |||
86 | return false; |
||
87 | } |
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: