Zend_Service_ShortUrl
Overview
URL shorteners have exploded in popularity in the last several years, in large part due
to the social nature of the web and the desire to share links.
Zend_Service_ShortUrl provides an API for accessing a number of
different URL shortener services, with the ability to both create short URLs as well as
retrieve the original URL.
Adapters provided include:
-
Zend_Service_ShortUrl_JdemCz, which accesses the » jdem.cz service.
-
Zend_Service_ShortUrl_TinyUrlCom, which accesses the » tinyurl.com service.
-
Zend_Service_ShortUrl_MetamarkNet, which accesses the » metamark.net service.
-
Zend_Service_ShortUrl_IsGd, which accesses the » is.gd service.
-
Zend_Service_ShortUrl_BitLy, which accesses the » bit.ly service.
Quick Start
Using one of the URL shortener services is straightforward. Each URL shortener follows a
simple interface that defines two methods: shorten() and
unshorten(). Instantiate the class, and call the appropriate
method.
$tinyurl = new Zend_Service_ShortUrl_TinyUrlCom();
// Shorten a URL:
$short = $tinyurl->shorten('http://framework.zend.com/'); // http://tinyurl.com/rxtuq
// Inflate or unshorten a short URL:
$long = $tinyurl->unshorten('http://tinyurl.com/rxtuq'); // http://framework.zend.com/
Available Methods
-
-
shorten
(
$url
)
-
Takes the given $url and passes it to the service in
order to obtain a shortened URL.
If the provided $url is invalid, an exception will be
raised.
-
-
unshorten
(
$shortenedUrl
)
-
Takes the provided $shortenedUrl and passes it to the
service in order to obtain the original URL.
If the provided $shortenedUrl is invalid, an exception
will be raised.
-
-
setHttpClient
(
Zend_Http_Client $httpClient
)
-
Use this method to set the HTTP client used for communicating with the
service.
-
-
getHttpClient
(
)
-
Use this method to access the HTTP client attached to the service. By
default, this will lazy-load an instance of
Zend_Http_Client if no client is yet attached.
Additional Bit.ly Methods
The bitly API require that authentication credentials be supplied as query arguments.
To get started, you'll need a free bitly user account and apiKey. Signup at:
» http://bitly.com/a/sign_up
bitly currently also supports the OAuth 2 draft specification. You could provide a
generated OAuth access token to Zend_Service_ShortUrl_BitLy
using setOAuthAccessToken or as constructor argument.
$bitly = new Zend_Service_ShortUrl_BitLy('username','apiKey');
// Shorten a URL:
$short = $bitly->shorten('http://framework.zend.com/'); // http://bit.ly/15Oe0
// Inflate or unshorten a short URL:
$long = $bitly->unshorten('http://bit.ly/15Oe0'); // http://framework.zend.com/
-
-
setApiLogin
(
$login, $apiKey
)
-
Takes the required $login username and $apiKey
and passes it to the service.
-
-
setOAuthAccessToken
(
$accessToken
)
-
Use the OAuth $accessToken for authentication