Skip to content

Instantly share code, notes, and snippets.

@gaffling
Last active March 31, 2021 13:24
Show Gist options
  • Save gaffling/002280071dc294b82368ec9190fc559b to your computer and use it in GitHub Desktop.
Save gaffling/002280071dc294b82368ec9190fc559b to your computer and use it in GitHub Desktop.
[URL Shortener] Shorten URL and preview shortend URL in long #php #function #urlshortener
/* ----------------------------------------------------------------------------------------- */
/* [URL Shortener] Shorten URL and preview shortend URL in long #php #function #urlshortener */
/* ----------------------------------------------------------------------------------------- */
function doShortURL($url) {
// thanks to tinyurl.com
return file_get_contents('http://tinyurl.com/api-create.php?url=' . $url);
}
function doLongURL($url) {
if (!function_exists("curl_init")) die("ERROR - Please install CURL on your php!");
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, TRUE);
@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = @curl_exec($ch);
#echo'<pre>';var_dump($response);
preg_match('/location: (.*)\n/i', $response, $a);
if (!isset($a[1])) return $url;
return $a[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment