- Add config/bitly.php file to your application/config folder.
- Add libraries/Bitly.php file into your application/libraries folder.
- CodeIgniter
- bit.ly login,
http://bit.ly/a/sign_up
- bit.ly apiKey,
http://bit.ly/a/your_api_key
Values used by the API can be set one of three ways
- by entering the values in the library's config file (i.e. config/bitly.php)
- by passing the values in an array when loading the library (i.e. $this->load->library('bitly', $params))
- by manually overriding prior values by passing an array to the set_config() method
$params = array( 'bitly_login' => 'login', 'bitly_apiKey' => 'apiKey', 'bitly_x_login' => '', 'bitly_x_apiKey' => '', 'bitly_format' => 'json', 'bitly_domain' => 'bit.ly' ); $this->bitly->set_config($params));
shorten( string $long_url)
if ($result = $this->bitly->shorten('http://www.example.com')) { // Parameters correctly set, $result will be whatever the API returns }
long_url : The URL that needs to be shortened
- JSON - The returned value will be the new bit.ly short URL. XML will return the entire XML response without parsing. TXT will return the bit.ly short URL.
- XML - The returned value will be the entire response string.
- TXT - The returned value will be the new bit.ly short URL.
expand( array $targets )
if ($result = $this->bitly->expand(array('http://bit.ly/bldm16', 'bldm16'))) { // Parameters correctly set, $result will be whatever the API returns }
targets : The bit.ly shortUrls or hashes that need to be expanded
- JSON - The returned value will be the long URL if there is only one URL or hash. If more than one URL or hash are passed, then the returned value is the
json_decoded()
response. - XML - The returned value will be the entire response string.
- TXT - The returned value will be the long URL.
validate( array $params )
if ($result = $this->bitly->validate(array('x_login' => 'notbilytapi', 'x_apiKey' => 'not_apikey'))) { // Parameters correctly set, $result will be whatever the API returns }
params : x_login and x_apiKey to be checked, passed as an array, if not set, config defaults will be used
- JSON - boolean TRUE or FALSE.
- XML - The returned value will be the entire response string.
- TXT - boolean TRUE or FALSE.
clicks( array $targets, [ string $type = 'user' ] )
if ($result = $this->bitly->clicks(array('http://bit.ly/bldm16', 'bldm16'))) { // Parameters correctly set, $result will be whatever the API returns }
targets : The bit.ly shortUrls or hashes to get the click information for
type : User will return the number of clicks for the particular user's hash, Global will return the total number of clicks for the URL
- JSON - The returned value will be the clicks if there is only one URL or hash. If more than one URL or hash are passed, then the returned value is the
json_decoded()
response. - XML - The returned value will be the entire response string.
- TXT - The returned value will be the clicks.
referrers( string $target )
if ($result = $this->bitly->referrers('http://bit.ly/bldm16') { // Parameters correctly set, $result will be whatever the API returns }
target : The bit.ly shortUrl or hash to get the referrer information for
- JSON - The returned value will be the referrers listed for the URL or hash.
- XML - The returned value will be the entire response string.
Note: 'txt' is not a supported format for this endpoint, if the default or passed format is 'txt', it will be changed to 'json'.
countries( string $target )
if ($result = $this->bitly->countries('http://bit.ly/bldm16') { // Parameters correctly set, $result will be whatever the API returns }
target : The bit.ly shortUrl or hash to get the country information for
- JSON - The returned value will be the countries listed for the URL or hash.
- XML - The returned value will be the entire response string.
Note: 'txt' is not a supported format for this endpoint, if the default or passed format is 'txt', it will be changed to 'json'.
clicks_by_minute( array $targets )
if ($result = $this->bitly->clicks_by_minute(array('http://bit.ly/bldm16', 'bldm16'))) { // Parameters correctly set, $result will be whatever the API returns }
targets : The bit.ly shortUrls or hashes to get the click information for
- JSON - The returned value will be the clicks if there is only one URL or hash. If more than one URL or hash are passed, then the returned value is the
json_decoded()
response. - XML - The returned value will be the entire response string.
Note: 'txt' is not a supported format for this endpoint, if the default or passed format is 'txt', it will be changed to 'json'.
clicks_by_day( array $targets, [ int $days = 7 ] )
if ($result = $this->bitly->clicks_by_minute(array('http://bit.ly/bldm16', 'bldm16'))) { // Parameters correctly set, $result will be whatever the API returns }
targets : The bit.ly shortUrls or hashes to get the click information for
days : The number of days of data that should be returned per URL or hash, limited from 1-30
- JSON - The returned value will be the clicks if there is only one URL or hash. If more than one URL or hash are passed, then the returned value is the
json_decoded()
response. - XML - The returned value will be the entire response string.
Note: 'txt' is not a supported format for this endpoint, if the default or passed format is 'txt', it will be changed to 'json'.
pro_domain( string $domain )
if ($result = $this->bitly->pro_domain(array('x_login' => 'notbilytapi', 'x_apiKey' => 'not_apikey'))) { // Parameters correctly set, $result will be whatever the API returns }
domain : Short domain to check
- JSON - boolean TRUE or FALSE.
- XML - The returned value will be the entire response string.
- TXT - boolean TRUE or FALSE.
lookup( array $targets )
if ($result = $this->bitly->lookup(array('http://example.com', 'example.com'))) { // Parameters correctly set, $result will be whatever the API returns }
targets : The URLs that need to be looked up
- JSON - The returned value will be the shortURL if there is only one URL. If more than one URL is passed, then the returned value is the
json_decoded()
response. - XML - The returned value will be the entire response string.
Notes: The HTTP scheme is automatically added onto a URL that has no scheme, as bit.ly always adds this when they shorten links. Also, while bit.ly will shorten a URL without a trailing slash, passing a URL with or without a trailing slash for Lookup may produce varied results. 'txt' is not a supported format for this endpoint, if the default or passed format is 'txt', it will be changed to 'json'.
info( array $targets )
if ($result = $this->bitly->info(array('http://bit.ly/bldm16', 'bldm16'))) { // Parameters correctly set, $result will be whatever the API returns }
targets : The bit.ly shortUrls or hashes to be queried
- JSON - The returned value will be the title of the website if there is only one URL or hash. If more than one URL or hash are passed, then the returned value is the
json_decoded()
response. - XML - The returned value will be the entire response string.
- TXT - The returned value will be the title of the website.
Depending on the return format, the return value will change. If a value is not correctly set, will return FALSE. After a call is executed, the full response text can be accessed by calling response()
.
For more information on the API, see http://code.google.com/p/bitly-api/wiki/ApiDocumentation
.
2012-07-23
- Converted library to Spark format.
- Reorganized repo's file structure, added spark.info file and updated README.md
2011-11-23
- Moved API target from library to config file
- Updated library with all additional functionality from API after original commit in April 2010 (see Revision history of API for details).
- Removed the 'verbose' flag from all methods. Verbose output (i.e. the full response) can be accessed by calling
response()
.