Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sclaeys
Created July 13, 2018 00:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sclaeys/dc6f1e68b84f2a7b9cb6a6675ad31a81 to your computer and use it in GitHub Desktop.
Save sclaeys/dc6f1e68b84f2a7b9cb6a6675ad31a81 to your computer and use it in GitHub Desktop.
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