Created
July 13, 2018 00:03
-
-
Save sclaeys/dc6f1e68b84f2a7b9cb6a6675ad31a81 to your computer and use it in GitHub Desktop.
Sample shortener API Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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