├── README.md
├── bitly.php
└── tests.php
/README.md:
--------------------------------------------------------------------------------
1 |
2 | This is a very simple PHP library for interacting with the v3 bit.ly api (supports OAuth endpoints). It should support all basic endpoints that are found/available via http://dev.bitly.com/api.html.
3 |
4 | ==============
5 | REQUIREMENTS:
6 | ==============
7 |
8 | PHP, Curl, JSON
9 |
10 | ======
11 | USAGE:
12 | ======
13 |
14 | 1. Simply download the bitly.php file and include it in your project directory
15 |
16 | 2. Make sure to include the file whenever you want to access the bitly api functionality... include_once('bitly.php');
17 |
18 | 3. Set up an associative array with the required parameters for the endpoint you want to access.
19 |
20 | 4. Make the bitly_get or bitly_post call, passing in the endpoint and the parameters as defined via the bit.ly API documentation ( http://dev.bitly.com/api.html ).
21 |
22 | =============
23 | EXAMPLES:
24 | =============
25 |
26 | ```php
27 | include_once('bitly.php');
28 | $params = array();
29 | $params['access_token'] = 'THE_TOKEN_SET_VIA_OAUTH';
30 | $params['longUrl'] = 'http://knowabout.it';
31 | $params['domain'] = 'j.mp';
32 | $results = bitly_get('shorten', $params);
33 | var_dump($results);
34 | ```
35 |
36 | ```php
37 | $params = array();
38 | $params['access_token'] = 'THE_TOKEN_SET_VIA_OAUTH';
39 | $params['url'] = 'http://knowabout.it';
40 | $results = bitly_get('link/lookup', $params);
41 | var_dump($results);
42 | ```
43 |
44 | a slightly more complex example with complex params (simply pass a third param of true when dealing with complex params):
45 |
46 | ```php
47 | $params = array();
48 | $params['access_token'] = 'THE_TOKEN_SET_VIA_OAUTH';
49 | $params['hash'] = array('dYhyia','dYhyia','abc123');
50 | $results = bitly_get('expand', $params, true);
51 | var_dump($results);
52 | ```
53 |
54 |
55 | You can find more detailed examples in the test.php file within this repo.
56 |
57 | =============
58 | SPECIAL NOTE:
59 | =============
60 |
61 | To use the new OAuth endpoints, you must first obtain an access token for a user. You do this by passing the user off to bit.ly to approve your apps access to their account...and then you use the return code along with the bitly_oauth_access_token method to obtain the actual bitly access token:
62 |
63 | 1. Present the user with a link as such:
64 |
65 | https://bit.ly/oauth/authorize?client_id=YOUR_BITLY_CLIENT_ID&redirect_uri=THE_URL_YOU_WANT_BITLY_TO_REDIRECT_TO_WHEN_APP_IS_APPROVED_BY_USER
66 |
67 | 2. a code ($_REQUEST['code']) will be supplied as a param to the url Bit.ly redirects to. So you can then execute:
68 |
69 | ```php
70 | $results = bitly_oauth_access_token($_REQUEST['code'],
71 | 'THE_URL_YOU_WANT_BITLY_TO_REDIRECT_TO_WHEN_APP_IS_APPROVED_BY_USER',
72 | 'YOUR_BITLY_APP_CLIENT_ID',
73 | 'YOUR_BITLY_APP_CLIENT_SECRET');
74 | ```
75 |
76 | 3. If everything goes correctly, you should now have a $results['access_token'] value that you can use with the oauth requests for that user.
77 |
78 | =======
79 | CONTACT:
80 | =======
81 |
82 | As always, if you've got any questions, comments, or concerns about
83 | anything you find here, please feel free to drop me an email at info@falicon.com or find me on Twitter @falicon
84 |
85 | =======
86 | License:
87 | =======
88 |
89 | Copyright 2015 Kevin Marshall.
90 |
91 | This program is free software: you can redistribute it and/or modify
92 | it under the terms of the GNU General Public License as published by
93 | the Free Software Foundation, either version 3 of the License, or
94 | (at your option) any later version.
95 |
96 | This program is distributed in the hope that it will be useful,
97 | but WITHOUT ANY WARRANTY; without even the implied warranty of
98 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
99 | GNU General Public License for more details.
100 |
101 | You should have received a copy of the GNU General Public License
102 | along with this program. If not, see .
103 |
104 |
--------------------------------------------------------------------------------
/bitly.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 |
12 | /**
13 | * The URI of the standard bitly v3 API.
14 | */
15 | define('bitly_api', 'http://api.bit.ly/v3/');
16 |
17 | /**
18 | * The URI of the bitly OAuth endpoints.
19 | */
20 | define('bitly_oauth_api', 'https://api-ssl.bit.ly/v3/');
21 |
22 | /**
23 | * The URI for OAuth access token requests.
24 | */
25 | define('bitly_oauth_access_token', 'https://api-ssl.bit.ly/oauth/');
26 |
27 | /**
28 | * Returns an OAuth access token as well as API users for a given code.
29 | *
30 | * @param $code
31 | * The OAuth verification code acquired via OAuth’s web authentication
32 | * protocol.
33 | * @param $redirect
34 | * The page to which a user was redirected upon successfully authenticating.
35 | * @param $client_id
36 | * The client_id assigned to your OAuth app. (http://bit.ly/a/account)
37 | * @param $client_secret
38 | * The client_secret assigned to your OAuth app. (http://bit.ly/a/account)
39 | *
40 | * @return
41 | * An associative array containing:
42 | * - login: The corresponding bit.ly users username.
43 | * - api_key: The corresponding bit.ly users API key.
44 | * - access_token: The OAuth access token for specified user.
45 | *
46 | * @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/oauth/access_token
47 | */
48 | function bitly_oauth_access_token($code, $redirect, $client_id, $client_secret) {
49 | $results = array();
50 | $url = bitly_oauth_access_token . "access_token";
51 | $params = array();
52 | $params['client_id'] = $client_id;
53 | $params['client_secret'] = $client_secret;
54 | $params['code'] = $code;
55 | $params['redirect_uri'] = $redirect;
56 | $output = bitly_post_curl($url, $params);
57 | $parts = explode('&', $output);
58 | foreach ($parts as $part) {
59 | $bits = explode('=', $part);
60 | $results[$bits[0]] = $bits[1];
61 | }
62 | return $results;
63 | }
64 |
65 | /**
66 | * Returns an OAuth access token via the user's bit.ly login Username and Password
67 | *
68 | * @param $username
69 | * The user's Bitly username
70 | * @param $password
71 | * The user's Bitly password
72 | * @param $client_id
73 | * The client_id assigned to your OAuth app. (http://bit.ly/a/account)
74 | * @param $client_secret
75 | * The client_secret assigned to your OAuth app. (http://bit.ly/a/account)
76 | *
77 | * @return
78 | * An associative array containing:
79 | * - access_token: The OAuth access token for specified user.
80 | *
81 | */
82 |
83 | function bitly_oauth_access_token_via_password($username, $password, $client_id, $client_secret) {
84 | $results = array();
85 | $url = bitly_oauth_access_token . "access_token";
86 |
87 | $headers = array();
88 | $headers[] = 'Authorization: Basic '.base64_encode($client_id . ":" . $client_secret);
89 |
90 | $params = array();
91 | $params['grant_type'] = "password";
92 | $params['username'] = $username;
93 | $params['password'] = $password;
94 |
95 | $output = bitly_post_curl($url, $params, $headers);
96 |
97 | $decoded_output = json_decode($output,1);
98 |
99 | $results = array(
100 | "access_token" => $decoded_output['access_token']
101 | );
102 |
103 | return $results;
104 | }
105 |
106 | /**
107 | * Format a GET call to the bit.ly API.
108 | *
109 | * @param $endpoint
110 | * bit.ly API endpoint to call.
111 | * @param $params
112 | * associative array of params related to this call.
113 | * @param $complex
114 | * set to true if params includes associative arrays itself (or using $val) {
126 | if (is_array($val)) {
127 | // we need to flatten this into one proper command
128 | $recs = array();
129 | foreach ($val as $rec) {
130 | $tmp = explode('/', $rec);
131 | $tmp = array_reverse($tmp);
132 | array_push($recs, $tmp[0]);
133 | }
134 | $val = implode('&' . $key . '=', $recs);
135 | }
136 | $url_params .= '&' . $key . "=" . $val;
137 | }
138 | $url = bitly_oauth_api . $endpoint . "?" . substr($url_params, 1);
139 | } else {
140 | $url = bitly_oauth_api . $endpoint . "?" . http_build_query($params);
141 | }
142 |
143 | //echo $url . "\n";
144 |
145 | $result = json_decode(bitly_get_curl($url), true);
146 |
147 | return $result;
148 | }
149 |
150 | /**
151 | * Format a POST call to the bit.ly API.
152 | *
153 | * @param $uri
154 | * URI to call.
155 | * @param $fields
156 | * Array of fields to send.
157 | */
158 | function bitly_post($endpoint, $params) {
159 | $result = array();
160 | $url = bitly_oauth_api . $api_endpoint;
161 | $output = json_decode(bitly_post_curl($url, $params), true);
162 | $result = $output['data'][str_replace('/', '_', $api_endpoint)];
163 | $result['status_code'] = $output['status_code'];
164 | return $result;
165 | }
166 |
167 | /**
168 | * Make a GET call to the bit.ly API.
169 | *
170 | * @param $uri
171 | * URI to call.
172 | */
173 | function bitly_get_curl($uri) {
174 | $output = "";
175 | try {
176 | $ch = curl_init($uri);
177 | curl_setopt($ch, CURLOPT_HEADER, 0);
178 | curl_setopt($ch, CURLOPT_TIMEOUT, 4);
179 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
180 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
181 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
182 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
183 | $output = curl_exec($ch);
184 | } catch (Exception $e) {
185 | }
186 | return $output;
187 | }
188 |
189 | /**
190 | * Make a POST call to the bit.ly API.
191 | *
192 | * @param $uri
193 | * URI to call.
194 | * @param $fields
195 | * Array of fields to send.
196 | */
197 | function bitly_post_curl($uri, $fields, $header_array = array()) {
198 | $output = "";
199 | $fields_string = "";
200 | foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
201 | rtrim($fields_string,'&');
202 | try {
203 | $ch = curl_init($uri);
204 |
205 | if(is_array($header_array) && !empty($header_array)){
206 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
207 | }
208 |
209 | curl_setopt($ch, CURLOPT_HEADER, 0);
210 | curl_setopt($ch,CURLOPT_POST,count($fields));
211 | curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
212 | curl_setopt($ch, CURLOPT_TIMEOUT, 2);
213 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
214 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
215 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
216 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
217 | $output = curl_exec($ch);
218 | } catch (Exception $e) {
219 | }
220 | return $output;
221 | }
222 |
223 | ?>
224 |
--------------------------------------------------------------------------------
/tests.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------