Tag Archives: JSON

How to use Site API to create short url?

22 Apr , 2009,
Administrator
, , , , ,
No Comments

Recently there was one requirement for one customer. where we have to post data to twitter application. but twitter when submitting comments on twittter, twitter having limitation that it has only 140 characters to be posted for status. out of that customer wants to post their blog’s url as well.

Now things started making complicated. I have suggested him to doc one short domain but later realize that he doesn’t have option to part domain for their hosting. so I have been told to use API for which will programmatically convert long url into short url. so I have added this blog so that people will understand how to create program which will dynamically covert long url into shorter one.

All the methods which I have suggested based on php programming.

Requirement :

I have used some of higher php function for that it needs you have 5.2.0+ and JSON is integrated. and Curl option is enable for your domain.

There has been 2 sites which we are working on http://cli.gs and http://bit.ly now you have registered their site. I hope they are still free and get Key as basically these are the keys which want while processing.

cli.gs

For This site you need to understand POST is not accepted by cligs. so you have to send data in get method.

<?php

$key = "abe34849f398d02b83abf64ed6690ef5a"; // This will be your key
$tobeShorten = "http://www.runwalsoft.com/blog/index.php";
// This will be url which you want to make it short
$url = "http://cli.gs/api/v1/cligs/create";
$param = "url=". urlencode($tobeShorten) ."&title=This is testing&key={$key}&appid=RunBlog";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url . "?" . $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
// Look at the returned header
$resultArray = curl_getinfo($ch);
curl_close($ch);

if($resultArray['http_code'] == 200)
{
  // This means that key is correct and the url which you have given is also working.
  echo "Short url is <b>" . $result . "</b>";
}
else
{
  echo "Error occured " . $resultArray['http_code'] ;
}

?>

Note : here key which I have written is dummy key. where you have to copy paste the key which you got from the site. and see you will get short url

Bit.ly

For Bit.ly there are few advantages and few are disadvantages. we can create 5 links concurently from same IP address. though that is really rare condition. advantage is that you can process multiple links at the same time. Means let say you want to convert 4 links in just one CURL call then it will be possible. and here you can use POST as well as GET mothod.

In both site you need to understand we have taken care of response from the server. if we got response header as 200 ( which means OK) then everything moving on is preety good. by default bit.ly provides response in JSON. I hope you know that thing. if you don’t know much just visit here its one of the simple method to process.

There is another option as well to process reponse as xml but I also still feel better to use JSON.

<?
$key = "R_c02b4ad85ccdsde92asdfe3951dfac08";
$username = "mrunwal" ;  // This is username when you register on their site.
$tobeShorten = "http://www.runwalsoft.com/blog/index.php";
$url = "http://api.bit.ly/shorten";

$param = "url=". urlencode($tobeShorten) ."&title=This is testing&key={$key}&appid=RunBlog";
$param = "version=2.0.1&longUrl=". urlencode($tobeShorten) ."&login=". $username ."&apiKey=" . $key ;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url . "?" . $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
// Look at the returned header
$resultArray = curl_getinfo($ch);
curl_close($ch);

//Following is the method to handle json output

//This function REQUIRES you should have php5.2.0 +
$result = json_decode($result,TRUE);
// This is the response which you gets from curl
/* "errorCode": 0, "errorMessage": "", "results": { "http://www.runwalsoft.com/blog/index.php":
{ "hash": "ptlxO","shortKeywordUrl": "", "shortUrl": "http://bit.ly/11vL1X", "userHash": "11vL1X" } },
"statusCode": "OK" */

if($resultArray['http_code'] == 200 && $result['errorCode'] == 0 && $result['statusCode'] == "OK")
{
  // This means that key is correct and the url which you have given is also working.
  echo "Short url is <b>" . $result['results'][$tobeShorten]['shortUrl'] . "</b>";
}
else
{
  echo "Error occured " . $resultArray['http_code'] ;
}

?>

Using this way it will be easier for you to process all shortenning of url. I hope you will feel better to use Runwalsoft’s programming for your current project. don’t botter to contact us.