Created
June 25, 2013 15:17
-
-
Save pinzler/5859319 to your computer and use it in GitHub Desktop.
Simple PHP library for interacting with the v3 BITLY API - using OAuth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Simple PHP library for interacting with the v3 BITLY API - using OAuth | |
* REQUIREMENTS: PHP, Curl, JSON | |
* | |
* @author Andrew Pinzler <[email protected]> | |
*/ | |
/** | |
* The URI of the bitly OAuth endpoints. | |
*/ | |
define('bitly_oauth_api', 'https://api-ssl.bit.ly/v3/'); | |
/** | |
* The bitly access token assigned to your bit.ly account. | |
*(http://bit.ly/a/oauth_apps or http://github.com/pinzler/bitly-php-oauth) | |
*/ | |
define('bitlyAccessToken', 'YOUR_BITLY_ACCESS_TOKEN'); | |
/** | |
* Given a longUrl, get the bit.ly shortened version - using Ouath | |
* | |
* | |
* @param $longUrl | |
* Long URL to be shortened. | |
* @param $domain | |
* Uses bit.ly (default), j.mp, or a bit.ly pro domain. | |
* @param $access_token | |
* User's Access Token. | |
* | |
* @return | |
* An associative array containing: | |
* - url: The unique shortened link that should be used, this is a unique | |
* value for the given bit.ly account. | |
* - hash: A bit.ly identifier for long_url which is unique to the given | |
* account. | |
* - global_hash: A bit.ly identifier for long_url which can be used to track | |
* aggregate stats across all matching bit.ly links. | |
* - long_url: An echo back of the longUrl request parameter. | |
* - new_hash: Will be set to 1 if this is the first time this long_url was | |
* shortened by this user. It will also then be added to the user history. | |
* | |
* @see http://dev.bitly.com/links.html#v3_shorten | |
*/ | |
function bitly_v3_shorten($longUrl, $domain = '') { | |
$result = array(); | |
$url = bitly_oauth_api . "shorten?access_token=" . bitlyAccessToken . "&format=json&longUrl=" . urlencode($longUrl); | |
if ($domain != '') { | |
$url .= "&domain=" . $domain; | |
} | |
$output = json_decode(bitly_get_curl($url)); | |
if (isset($output->{'data'}->{'hash'})) { | |
$result['url'] = $output->{'data'}->{'url'}; | |
$result['hash'] = $output->{'data'}->{'hash'}; | |
$result['global_hash'] = $output->{'data'}->{'global_hash'}; | |
$result['long_url'] = $output->{'data'}->{'long_url'}; | |
$result['new_hash'] = $output->{'data'}->{'new_hash'}; | |
} | |
return $result; | |
} | |
/** | |
* Make a GET call to the bitly 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, 25); | |
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 bitly API. | |
* | |
* @param $uri | |
* URI to call. | |
* @param $fields | |
* Array of fields to send. | |
*/ | |
function bitly_post_curl($uri, $fields) { | |
$output = ""; | |
$fields_string = ""; | |
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } | |
rtrim($fields_string,'&'); | |
try { | |
$ch = curl_init($uri); | |
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, 25); | |
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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment