-
Notifications
You must be signed in to change notification settings - Fork 134
/
bitly.php
223 lines (201 loc) · 6.1 KB
/
bitly.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* @file
* Simple PHP library for interacting with the v3 bit.ly api (only deals with
* JSON format, but supports new OAuth endpoints).
* REQUIREMENTS: PHP, Curl, JSON
*
* @link https://github.com/Falicon/BitlyPHP
* @author Kevin Marshall <info@falicon.com>
*/
/**
* The URI of the standard bitly v3 API.
*/
define('bitly_api', 'http://api.bit.ly/v3/');
/**
* The URI of the bitly OAuth endpoints.
*/
define('bitly_oauth_api', 'https://api-ssl.bit.ly/v3/');
/**
* The URI for OAuth access token requests.
*/
define('bitly_oauth_access_token', 'https://api-ssl.bit.ly/oauth/');
/**
* Returns an OAuth access token as well as API users for a given code.
*
* @param $code
* The OAuth verification code acquired via OAuth’s web authentication
* protocol.
* @param $redirect
* The page to which a user was redirected upon successfully authenticating.
* @param $client_id
* The client_id assigned to your OAuth app. (http://bit.ly/a/account)
* @param $client_secret
* The client_secret assigned to your OAuth app. (http://bit.ly/a/account)
*
* @return
* An associative array containing:
* - login: The corresponding bit.ly users username.
* - api_key: The corresponding bit.ly users API key.
* - access_token: The OAuth access token for specified user.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/oauth/access_token
*/
function bitly_oauth_access_token($code, $redirect, $client_id, $client_secret) {
$results = array();
$url = bitly_oauth_access_token . "access_token";
$params = array();
$params['client_id'] = $client_id;
$params['client_secret'] = $client_secret;
$params['code'] = $code;
$params['redirect_uri'] = $redirect;
$output = bitly_post_curl($url, $params);
$parts = explode('&', $output);
foreach ($parts as $part) {
$bits = explode('=', $part);
$results[$bits[0]] = $bits[1];
}
return $results;
}
/**
* Returns an OAuth access token via the user's bit.ly login Username and Password
*
* @param $username
* The user's Bitly username
* @param $password
* The user's Bitly password
* @param $client_id
* The client_id assigned to your OAuth app. (http://bit.ly/a/account)
* @param $client_secret
* The client_secret assigned to your OAuth app. (http://bit.ly/a/account)
*
* @return
* An associative array containing:
* - access_token: The OAuth access token for specified user.
*
*/
function bitly_oauth_access_token_via_password($username, $password, $client_id, $client_secret) {
$results = array();
$url = bitly_oauth_access_token . "access_token";
$headers = array();
$headers[] = 'Authorization: Basic '.base64_encode($client_id . ":" . $client_secret);
$params = array();
$params['grant_type'] = "password";
$params['username'] = $username;
$params['password'] = $password;
$output = bitly_post_curl($url, $params, $headers);
$decoded_output = json_decode($output,1);
$results = array(
"access_token" => $decoded_output['access_token']
);
return $results;
}
/**
* Format a GET call to the bit.ly API.
*
* @param $endpoint
* bit.ly API endpoint to call.
* @param $params
* associative array of params related to this call.
* @param $complex
* set to true if params includes associative arrays itself (or using <php5)
*
* @return
* associative array of bit.ly response
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/validate
*/
function bitly_get($endpoint, $params, $complex=false) {
$result = array();
if ($complex) {
$url_params = "";
foreach ($params as $key => $val) {
if (is_array($val)) {
// we need to flatten this into one proper command
$recs = array();
foreach ($val as $rec) {
$tmp = explode('/', $rec);
$tmp = array_reverse($tmp);
array_push($recs, $tmp[0]);
}
$val = implode('&' . $key . '=', $recs);
}
$url_params .= '&' . $key . "=" . $val;
}
$url = bitly_oauth_api . $endpoint . "?" . substr($url_params, 1);
} else {
$url = bitly_oauth_api . $endpoint . "?" . http_build_query($params);
}
//echo $url . "\n";
$result = json_decode(bitly_get_curl($url), true);
return $result;
}
/**
* Format a POST call to the bit.ly API.
*
* @param $uri
* URI to call.
* @param $fields
* Array of fields to send.
*/
function bitly_post($endpoint, $params) {
$result = array();
$url = bitly_oauth_api . $api_endpoint;
$output = json_decode(bitly_post_curl($url, $params), true);
$result = $output['data'][str_replace('/', '_', $api_endpoint)];
$result['status_code'] = $output['status_code'];
return $result;
}
/**
* Make a GET call to the bit.ly API.
*
* @param $uri
* URI to call.
*/
function bitly_get_curl($uri) {
$output = "";
try {
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$output = curl_exec($ch);
} catch (Exception $e) {
}
return $output;
}
/**
* Make a POST call to the bit.ly API.
*
* @param $uri
* URI to call.
* @param $fields
* Array of fields to send.
*/
function bitly_post_curl($uri, $fields, $header_array = array()) {
$output = "";
$fields_string = "";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
rtrim($fields_string,'&');
try {
$ch = curl_init($uri);
if(is_array($header_array) && !empty($header_array)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$output = curl_exec($ch);
} catch (Exception $e) {
}
return $output;
}
?>