Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample shortener API Function
<?php
define("SHORTENER_URL", "http://webz.cc");
define("SHORTENER_KEY", "123456789");
/**** Sample PHP Function ***/
function shorten($url, $custom = "", $format = "json") {
$api_url = SHORTENER_KEY."/api/?key=".SHORTENER_KEY;
$api_url .= "&url=".urlencode(filter_var($url, FILTER_SANITIZE_URL));
if(!empty($custom)){
$api_url .= "&custom=".strip_tags($custom);
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $api_url
));
$Response = curl_exec($curl);
curl_close($curl);
if($format == "text"){
$Ar = json_decode($Response,TRUE);
if($Ar["error"]){
return $Ar["msg"];
}else{
return $Ar["short"];
}
}else{
return $Response;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment