-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGeoloqi.php
126 lines (99 loc) · 3.4 KB
/
Geoloqi.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
class Geoloqi {
const VERSION = '1.1.0';
const API_URL = 'https://api.geoloqi.com/1/';
const OAUTH_URL = 'https://geoloqi.com/oauth/authorize';
private $clientID;
private $clientSecret;
private $redirectURI;
private $accessToken;
private $auth;
public static function createWithAccessToken($accessToken) {
$auth = new StdClass;
$auth->access_token = $accessToken;
return new self(null, null, null, $auth);
}
public function __construct($clientID, $clientSecret, $redirectURI, $auth=null) {
$this->clientID = $clientID;
$this->clientSecret = $clientSecret;
$this->redirectURI = $redirectURI;
$this->auth = $auth;
}
public function get($path, $args=null, $headers=array()) {
return $this->execute('GET', $path, $args, $headers);
}
public function post($path, $args=null, $headers=array()) {
return $this->execute('POST', $path, $args, $headers);
}
public function execute($method, $path, $args=null, $headers=array()) {
if($this->accessToken() !== null) {
$headers[] = 'Authorization: OAuth '.$this->auth->access_token;
}
$response = $this->executeLowLevel($method, $path, $args, $headers);
return json_decode($response);
}
/* This is abstracted from execute so you can get the raw return if you need to. */
public function executeLowLevel($method, $path, $args=null, $headers=array()) {
$ch = curl_init();
$defaultHeaders = array('Accept: application/json',
'Content-Type: application/json',
'User-Agent: geoloqi-sdk-php '.self::VERSION);
$headers = array_merge($defaultHeaders, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($args ? json_encode($args) : ''));
} else if ($method === 'GET') {
if (is_array($args)
&& count($args) > 0) {
$path .= '?' . http_build_query($args);
}
}
curl_setopt($ch, CURLOPT_URL, self::API_URL.$path);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function isLoggedIn() {
return(isset($this->auth->access_token));
}
public function accessToken() {
if(isset($this->auth->access_token)) {
return $this->auth->access_token;
} else {
return null;
}
}
public function login($args=array()) {
$defaultArgs = array('response_type' => 'code',
'client_id' => $this->clientID,
'redirect_uri' => $this->redirectURI);
$args = array_merge($defaultArgs, $args);
$oauthUrl = self::OAUTH_URL.'?'.http_build_query($args);
header('Location: '.$oauthUrl);
exit;
}
public function establish($opts=array()) {
return $this->post('oauth/token', $opts);
}
public function getAuthWithCode($code) {
$auth = $this->establish(array('grant_type' => 'authorization_code',
'client_id' => $this->clientID,
'client_secret' => $this->clientSecret,
'code' => $code,
'redirect_uri' => $this->redirectURI));
$this->auth = $auth;
return $this->auth;
}
public function auth() {
return $this->auth;
}
public function setAuth($auth) {
$this->auth = $auth;
}
public function logout() {
$this->auth = null;
}
}
?>