I have a website and i need to make a feature that when a user post a link(s) it will become short, by means of a url shortening service API.
Here's the API using Php JSON
$api_url="http://s.ourbyte.org/api?api=cSuPDdLzHmPq&url=$message";
$res= @json_decode(file_get_contents($api_url),TRUE);
if($res["error"]){
echo $res["msg"];
}else{
echo $res["short"];
}
}
And here's the function where the message will be posted.
function postMessage($message, $image, $type, $value, $privacy) {
global $LNG;
list($error, $content) = $this->validateMessage($message, $image, $type, $value, $privacy);
if($error) {
// Randomize a number for the js function
$rand = rand();
$switch = ($content[2]) ? sprintf($LNG["{$content[0]}"], $content[2], $content[1]) : sprintf($LNG["{$content[0]}"], $content[1]);
return $this->db->real_escape_string('<div class="message-container" id="notification'.$rand.'"><div class="message-content"><div class="message-inner">'.$switch.'<div class="delete_btn" title="Dismiss" onclick="deleteNotification(0, \''.$rand.'\')"></div></div></div></div>');
} else {
// Add the insert message
$stmt = $this->db->prepare("$content");
// Execute the statement
$stmt->execute();
// Save the affected rows
$affected = $stmt->affected_rows;
// Close the statement
$stmt->close();
// If the comment was added, return 1
if($affected) {
return $this->db->real_escape_string($this->getLastMessage());
} else {
return '<div class="message-container" id="notification'.$rand.'"><div class="message-content"><div class="message-inner">'.$LNG['unexpected_message'].'<div class="delete_btn" title="Dismiss" onclick="deleteNotification(0, \''.$rand.'\')"></div></div></div></div>';
}
}
}