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>';
            }
        }
    } 

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

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.

Member Avatar for diafol

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

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

Member Avatar for diafol

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

Why? If you don't mind me asking.

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

Member Avatar for diafol

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.