0

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>';
            }
        }
    } 
5
Contributors
10
Replies
52
Views
3 Years
Discussion Span
Last Post by JorgeM
Featured Replies
  • 1
    diafol 3,720   3 Years Ago

    Url shotening may be *de rigeur*, but they do have their pitfalls. If you use a third party API for this, ensure that the party has a good chance of lasting a good while. If the service goes belly-up, you could be left with every link in your messages being … Read More

0

how can i integrate it so when a user post a link it will be shorten by the url shortener.

0

Sort of, $message holds the value of the post of a user
example : "check this new site http://www.daniweb.com"
so i want to replace automatically that link in the message by the short url service.

0

Why do you want to obscure the link? Are you limited to space?

0

no not that. i just want to impliment that feature.

Why? If you don't mind me asking.

0

Pretty simple. You parse the message for URLs. The easy way to do this is to use regular expression and match urls using the preg_match() function. Once you've done that you will have an array containing all matches (URLs). You call the function having your API code in a for loop until all URLs are shortened and then put it in the database i.e. the else part in your code(with prepare and execute) executes. Make sure you put the API functions in a neat public function which returns the generated short URL alone! Hope this helps

1

Url shotening may be de rigeur, but they do have their pitfalls. If you use a third party API for this, ensure that the party has a good chance of lasting a good while. If the service goes belly-up, you could be left with every link in your messages being totaly useless. Some services have an API that is free to use, but they usually limit you to a certain number of entries per day/week/month. Some you can pay for to increase the number of entries. Some may even allow you to use your own unique shotened domain - but you probably need to pay for this. You could, if you wanted to, create your own service from scratch. Before implementing any shortening, decide whether you really need to do so. From a personal point, I get a bit twitchy when I click on a shortened link as I have no idea to where I'll be taken. At least with a full url, I can at least see the domain.

The visible text of the link can be anything, as you know, so if you allow this - as opposed to displaying the link url - url shortening becomes pretty superfluous.

Just my 2p.

Edited by diafol

0

I get a bit twitchy when I click on a shortened link as I have no idea to where I'll be taken

I feel the same way, I tend to stay away from these links. It makes me feel that someone is trying to hide something from me. It could be mental, but I tend to think many people feel this way.

I guess you have to consider who you audience is going to be. If this is perfectly acceptable for your users, then you should be good to go.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.