-
Notifications
You must be signed in to change notification settings - Fork 7.6k
isgdURL
Derek Jones edited this page Jul 5, 2012
·
6 revisions
isgdURL creates a shortened url using the is.gd service's API.
is.gd's API documentation is found at http://74.208.111.132/gb/is.gd/api_info.php
TODO: we can verify that the shorted url points correctly by getting the headers returned by a curl request. When requesting the shortened URL, we should get HTTP 301 redirect header from is.gd, with the location parameter set to the original long URL. We need only request the headers back from is.gd in our curl request, not the actual page, so verification should be fairly quick and painless.
use :
<?php
anchor(isgdURL($longurl));
?>
/**
* @category helpers
* @author Nathan Letsinger
* @copyright Copyright (c) 2009, Nathan Letsinger
* @license http://www.gnu.org/copyleft/gpl.html
*
* creates a shortened url using is.gd's API when supplied with a url string
*
* WARNING:
* This function expects a well formed unencoded url and does not validate input.
* However, is.gd does validate input, and will return an error message on malformed
* urls like " Error: The URL entered was not valid." along with a 500 status code header.
*
* PHP REQUIREMENTS:
* This helper requires the cURL library (libcurl) to be compiled into PHP to reach is.gd
* if curl is not installed you might opt to replace curl calls with a single
* call to file_get_contents if the fopen wrappers have been enabled. However
* using that function may take significantly longer (noticibly in seconds) and
* degrade performance.
*
* CI REQUIREMENTS:
* Requires the xss_clean method of CI_input class to parse url output from is.gd
* if xss_clean cannot be found the function returns the original input $url.
*
* TERMS OF SERVICE REMINDER:
* Note that users of the is.gd API are expected to comply with TOS
* found here: http://74.208.111.132/gb/is.gd/terms.php
*
* @link http://us3.php.net/manual/en/intro.curl.php PHP-cURL
* @link http://74.208.111.132/gb/is.gd/api_info.php API-doc
* @link http://74.208.111.132/gb/is.gd/terms.php API-Terms-of-Service
* @uses CI_Input::xss_clean()
* @param string $url unencoded url to shrink
* @return string either the newly shrunk url or $url
*/
function isgdURL($url)
{
$timeout_seconds = 5; //@todo finetune timeout in seconds for your app.
$curl_handle = curl_init('http://is.gd/api.php?longurl=' . urlencode($url));
curl_setopt($curl_handle, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, $timeout_seconds);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$shrunkURL = curl_exec($curl_handle);
$http_response_int = curl_getinfo($curl_handle , CURLINFO_HTTP_CODE);
if (curl_errno($curl_handle))
{
log_message('error','curl error: ' . curl_error($curl_handle));
return $url;
}
curl_close($curl_handle);
/**
* is.gd responds w/ header status code "HTTP/1.1 200 OK" if url is shortened as expected
* and w/ "HTTP/1.1 500 Internal Server Error" if there is a problem.
* We return the supplied url on any status code but 200 & that matches
* the expected output pattern.
*/
switch ($http_response_int)
{
case 200:
if( ! preg_match("/^http(s?):\/\/is.gd\/[a-z0-9]+/i" , $shrunkURL))
{
log_message('error','Failed preg match with shrunk url ' . $shrunkURL);
return $url;
}
break;
case 500:
log_message('error','500 Internal Server Error contacting is.gd api with url "' . $url . '"');
log_message('error','is.gd api responded : ' . $shrunkURL);
return $url;
break;
default:
log_message('error','Response status ' . $http_response_int . ' contacting is.gd api with url "' . $url . '"');
return $url;
}
$ci =& get_instance();
return (method_exists($ci->input, 'xss_clean')) ? $ci->input->xss_clean($shrunkURL) : $url ;
}