Completed
Push — master ( f608af...509103 )
by Iurii
01:13
created

example.php ➔ request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 3
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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
Coding Style introduced by
fetch uses the super-global variable $_SESSION which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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
}