-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuntappdPHP.php
164 lines (121 loc) · 3.84 KB
/
untappdPHP.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
UntappdPHP 1.0
By: Greg Avola (@gregavola)
Inspired By: https://github.com/abraham/twitteroauth
*/
class UntappdPHP {
public $access_token = "";
public $client_id = "";
public $client_secret = "";
public $callback_url = "";
public $userAgent = "UntappdPHP-GH-1.0";
public $http_code;
public $apiBase = "https://api.untappd.com/v4";
public $authenticateURL = "https://untappd.com/oauth/authenticate";
public $authorizeURL = "https://untappd.com/oauth/authorize";
// Construct for the Class. Consumer Key and Consumer Secret are required, however you can pass through the token if you already need them.
public function __construct($client_id, $client_secret, $redirect_url = "", $access_token = NULL)
{
$this->client_id = $client_id;
$this->client_secret = $client_secret;
$this->access_token = $access_token;
$this->redirect_url = $redirect_url;
}
public function setToken($access_token) {
$this->access_token = $access_token;
}
/**
* Get the authorize URL, you must pass in the $token paramater
*
* @returns a string
*/
function getAuthenticateURL() {
return $this->authenticateURL . "?client_id=".$this->client_id."&client_secret=".$this->client_secret."&redirect_url=".$this->redirect_url;
}
/**
* Get the authorize URL, you must pass in the $token paramater
*
* @returns a string
*/
function getAccessToken($code) {
$params = array(
"client_id" => $this->client_id,
"client_secret" => $this->client_secret,
"redirect_url" => $this->redirect_url,
"code" => $code
);
$url = $this->authorizeURL . "?" . http_build_query($params);
$response = $this->call($url, "POST", array());
return json_decode($response);
}
/**
* Basic GET wrapper for non-oauth calls to the API
*
*/
public function get($url, $params = array()) {
$added = "";
if (sizeof($params) != 0) {
$added = "&" . http_build_query($params);
}
if ($this->access_token == "") {
if(stristr($url, '?') === FALSE) {
$url = $this->apiBase . $url . "?client_id=".$this->client_id . "&client_secret=" . $this->client_secret . $added;
}
else {
$url = $this->apiBase . $url . "&client_id=".$this->client_id . "&client_secret=" . $this->client_secret . $added;
}
}
else {
$url = $this->apiBase . $url . "?access_token=".$this->access_token . $added;
}
$response = $this->call($url, 'GET', $params);
return json_decode($response);
}
/**
* Basic GET wrapper for oauth calls to the API
*
*/
public function post($url, $params = array()) {
if ($this->access_token == "") {
if(stristr($url, '?') === FALSE) {
$url = $this->apiBase . $url . "?client_id=".$this->client_id . "&client_secret=" . $this->client_secret;
}
else {
$url = $this->apiBase . $url . "&client_id=".$this->client_id . "&client_secret=" . $this->client_secret;
}
}
else {
if(stristr($url, '?') === FALSE) {
$url = $this->apiBase . $url . "?access_token=".$this->access_token;
}
else {
$url = $this->apiBase . $url . "&access_token=".$this->access_token;
}
}
$response = $this->call($url, 'POST', $params);
return json_decode($response);
}
/**
* Basic CURL request which connects to the Tumblr API URLs and returns the result
*
* @returns result from the URL call
*/
private function call($url, $method, $parameters) {
$curl2 = curl_init();
if ($method == "POST")
{
curl_setopt($curl2, CURLOPT_POST, true);
curl_setopt($curl2, CURLOPT_POSTFIELDS, $parameters);
}
curl_setopt($curl2, CURLOPT_URL, $url);
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl2, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl2, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl2, CURLOPT_USERAGENT, $this->userAgent);
$result = curl_exec($curl2);
$HttpCode = curl_getinfo($curl2, CURLINFO_HTTP_CODE);
$this->http_code = $HttpCode;
return $result;
}
}