API

Create a mobile tiny url

The API for mtny.mobi is a simple rest-like service. You do a http get to the url below, and get xml or json in return.

The request

The url to the api is http://mtny.mobi/api/
The following parameters are accepted:
 url     The url you want to shorten. Including http://
 type What kind of response you would like. Three types available; xml,simple and json or jsonp. Default is simple. Simple means that the body of the http response contains the shortened url.
 ismobile  Whether the url you are submitting supports mobile devices. Legal values; true or false. Default is false. This variable decides whether a mobile device is offered optimized presentation.

If you for example want to shorten this url http://edition.cnn.com/2009/HEALTH/01/04/true.love.found/index.html , the request would look like this:
http://mtny.mobi/api/?url=http%3A%2F%2Fedition.cnn.com%2F2009%2FHEALTH%2F01%2F04%2Ftrue.love.found%2Findex.html&ismobile=false&type=xml
The above request will return an xml response. To change it to a json response change the value of type to json:
http://mtny.mobi/api/?url=http%3A%2F%2Fedition.cnn.com%2F2009%2FHEALTH%2F01%2F04%2Ftrue.love.found%2Findex.html&ismobile=false&type=json

The response

The response contains 4 elements:

 status   A status code of the operation. 200 means All OK.
 message  A descriptive message. 
 url  The shortened url.
 target_supports_mobile  Whether the target url supports mobile.

Example json response:
{"status":"200","message":"OK, the shortened url is created. You saved 48 characters","url":"http:\/\/mtny.mobi\/2Y","target_supports_mobile":"false"}

If you want to use the json response within your ajax application you probably want to use the jsonp 
version. Then the json is wrapped in a callback function of your choice:
http://mtny.mobi/api/?url=http%3A%2F%2Fedition.cnn.com%2F2009%2FHEALTH%2F01%2F04%2Ftrue.love.found%2Findex.html&ismobile=false&type=jsonp&callback=mycallback
Will give you this response:
mycallback({"status":"200","message":"OK, the shortened url is created. You saved 48 characters","url":"http:\/\/mtny.mobi\/2Y","target_supports_mobile":"false"})

Example xml response:
<?xml version="1.0"?>
<mtny>
<status>200</status>
<message>OK, the shortened url is created. You saved 48 characters</message>
<url target_supports_mobile="false"><![CDATA[http://mtny.mobi/2Y]]></url>
</mtny>

Example php code

<?php

echo get_mtny_url("http://google.com/","xml");
echo get_mtny_url("http://google.com/","json");
echo get_mtny_url("http://google.com/");

function get_mtny_url($url,$type="simple",$ismobile="false"){
  if(preg_match('/^(http|https|ftp):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}' . '((:[0-9]{1,5})?\/.*)?$/i' ,$url)){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,"http://mtny.mobi/api/?type=$type&ismobile=$ismobile&url=".urlencode($url));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $mtny = curl_exec($ch);
    curl_close($ch);
 
    $r = false; 
    switch($type){
      case "xml":
        try{
          $xml = new SimpleXMLElement($mtny);
          if($xml->status =="200")
            $r = $xml->url;
        }catch (Exception $e) {
          $r=false;
        }
        break;
      case "json":
        $jsn = json_decode($mtny);
        if($jsn->{'status'} =="200")
          $r = $jsn->{'url'};
        break;
      default:
        $r =  trim($mtny);
    }
    return $r;
  }else
    return false;
}


Example AJAX code using jquery

<script>
$.getJSON("http://mtny.mobi/api/?type=jsonp&url=http://cnn.com&callback=?",
   function(s){
      alert(s.status);
   }
);
</script>

This code will create a tiny url and alert the status of the operation.

Look up a mobile tiny url

The url to the lookup function is http://mtny.mobi/api/lookup.php

The request

A request to lookup the url http://mtny.mobi/1 looks like this:

http://mtny.mobi/api/lookup.php?id=1

You can decide the formatting of you response as well, by appending &type=xml to your url:

http://mtny.mobi/api/lookup.php?id=1&type=xml


 id The  "key" for the shortened url. Example: When the shortened url is http://mtny.mobi/2e3, the value of the id parameter is 2e3.
 type What kind of response you would like. Three types available; xml,simple and json. Default is simple. Simple means that the body of the http response contains the url. Simple is default

The response

Example json response:
{"status":"200","message":"Cool! Found the url for '1'; Created 2008-11-14 13:23:24; Last accessed: 2009-03-05 06:38:21; Total fwds:111","url":"https:\/\/sites.google.com\/site\/mtnymobi2\/Home","target_supports_mobile":"false"}
Example xml response:
<?xml version="1.0"?>
<mtny>
<status>200</status>
<message>Cool! Found the url for '1'; Created 2008-11-14 13:23:24; Last accessed: 2009-03-05 06:38:21; Total fwds:111</message>
<url target_supports_mobile="false"><![CDATA[https://sites.google.com/site/mtnymobi2/Home]]></url>
</mtny>
"Simple" response returns the url as plain text.

 status   A status code of the operation. 200 means All OK, 404 means the url cannot be found
 message  A descriptive message. 
 url  The long url
 target_supports_mobile  Whether the target url supports mobile.

Authentication

There is no authentication currently. A simple "key exchange" solution may be implemented in the future.

Comments