Created
April 9, 2013 17:04
-
-
Save nilsding/5347435 to your computer and use it in GitHub Desktop.
A small API for lilURL. Place in the same folder as your index.php.
It uses a HTTP POST request for the long url and outputs a JSON string as response.
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 | |
/* api.php - A small API hack for lilURL | |
* Made in 2013 by @nilsding | |
*/ | |
require_once 'includes/conf.php'; | |
require_once 'includes/lilurl.php'; | |
$lilurl = new lilURL(); | |
$msg = ''; | |
if ( isset($_POST['longurl']) ) | |
{ | |
$longurl = trim(mysql_escape_string($_POST['longurl'])); | |
$protocol_ok = false; | |
if ( count($allowed_protocols) ) | |
{ | |
foreach ( $allowed_protocols as $ap ) | |
{ | |
if ( strtolower(substr($longurl, 0, strlen($ap))) == strtolower($ap) ) | |
{ | |
$protocol_ok = true; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
$protocol_ok = true; | |
} | |
if ( $protocol_ok && $lilurl->add_url($longurl) ) | |
{ | |
if ( REWRITE ) | |
{ | |
$url = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']).$lilurl->get_id($longurl); | |
} | |
else | |
{ | |
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?id='.$lilurl->get_id($longurl); | |
} | |
$msg = '{"success": true, "status": "Successfully shortened url", "url": "'.$url.'"}'; | |
} | |
elseif ( !$protocol_ok ) | |
{ | |
$msg = '{"success": false, "status": "Malformed URL (has to start with http:// or https://"}'; | |
} | |
else | |
{ | |
$msg = '{"success": false, "status": "Could not shorten URL due to an unknown error"}'; | |
} | |
} | |
else | |
{ | |
$msg = '{"success": false, "status": "To use this URL shortener\'s API, you need to provide a POST parameter ' . | |
'\'longurl\', which contains the URL which will be shortened by the service.", "exampleCURLCommand": ' . | |
'"curl -F longurl=\'http://twitter.com/nilsding\' http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'"}'; | |
} | |
echo $msg; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment