Create REST API Using Slim PHP Framework

I prefer slim framework for creating REST APIs because of two main reasons :

  1. It’s very lightweight and focused on REST and nothing else.
  2. It supports all the HTTP methods (GET, POST, PUT, DELETE).

I will create an API which will accept the JSON Data and store it into a file. Using GET Request you get these data in json format.

First you download the slim framework from here . Unzip the file and put it inside your server. Replace the contents of index.php .

require 'Slim/Slim.php';
$app = new \Slim\Slim(array(
"MODE" => "development"
$app->post('/test', 'test');
$app->get('/test', 'get_data');
function test() {
 $request = file_get_contents('php://input');    //get the body of the http request
// $data_arr =json_decode($request,true);  //convert the json into array, you can manipulate the data the you you want
//store the json in a file
 $handle = fopen('data/test_data.json', 'w+');  // you must create a data folder in the same folder of index.php
 fwrite($handle, $request); 
echo "Success";
function get_data(){
$data=file_get_contents("data/test_data.json");  //read the json data from the file
echo $data; // return the data

Now your api is  ready. You can test it using any rest client.
You can explore slim framework Documentation to create more complicated api.

You can also test it using curl -
curl -i -X POST -d '{"blog" : "codegem"}' localhost/test

Develop REST API Using Sinatra (A Ruby Framework)

This REST API accepts JSON  Data and stores it in a file. You can retrieve these data using GET Request.

require 'rubygems'
require 'sinatra'
require 'sinatra/json'
require 'json'
require 'sinatra/cross_origin'
configure do
 enable :cross_origin
set :environment, :production

post '/test' do
 cross_origin :allow_origin => '*',
 :allow_methods => [:post],
 :allow_credentials => true,
 :max_age => "60"
 data = JSON.parse(
 aFile ="data/data.json", "w+")
if aFile
 json data

get '/test' do
 cross_origin :allow_origin => '*',
 :allow_methods => [:get],
 :allow_credentials => true,
 :max_age => "60"
 aFile ="data/data.json", "r+")
if aFile
 content = aFile.sysread(100)
 json content

You can test this api using REST Clients (Browser extensions).

You can also test it using curl -
 curl -i -X POST -d '{"blog" : "codegem"}' localhost:4567/test

Getting Started With Sinatra (Ruby Framework) on Windows

Sinatra is a free and open source software web application library and domain-specific language written in Ruby. It is an alternative to other Ruby web application frameworks such as Ruby on Rails, Merb, Nitro and Camping.
Getting Started With Sinatra (For Development)
1. Install latest version of Ruby : You can download the windows installer from
2. Set path : C:\(ruby install directory)\bin;
3. Go to command promt. Type this command to install Sinatra : gem install Sinatra (if path is not set then go to C:\Ruby193\bin and then run the command from cmd).
4. Install Sinatra_contrib : gem install sinatra-contrib (after installing this you need not to restart the server with every change in your file)

Now you are ready to build your first app with Sinatra.
Step 1: create your project directory “myproject” anywhere in your computer.
Step 2: create a new file test.rb and add these lines

 require 'rubygems'
 require 'sinatra'
get '/firstapp' do
 "Welcome to my first Sinatra App !"

Step 3 : In command Prompt, traverse to you project directory and run this command
ruby test.rb
If your path is not set then you will have to run it from C:\Ruby193\bin directory.
Step 4 : Now go to http://localhost:4567/firstapp

You will see the webpage with message “Welcome to my first Sinatra App !”

So you have Created your First App In Sinatra

Client side IP geolocation using JavaScript

<h3>Client side IP geolocation using <a href=""></a></h3>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>

<script type="text/javascript">
$.get("", function (response) {
 $("#ip").html("IP: " + response.ip);
 $("#address").html("Location: " + + ", " + response.region);
 $("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");

OS Detection in JavaScript

You can easily get the operating System of your visitor in javascript. Here is the javascript code which will give you the operating system of your visitor.

<script type="text/javascript">
var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

Getting Visitor’s Country in JavaScript

In some of the applications we may need to find the visitor’s location like City, Country,etc. Thereb is a very easy to use service provided by . The GeoNames geographical database covers all countries and contains over eight million placenames that are available for download free of charge.

Here is a Jquery Code which will give you the visitor’s Country and Country Code. You will need the updated version of jquery as well.

<script type="text/javascript">
if (navigator.geolocation) {
 navigator.geolocation.getCurrentPosition(function(position) {
 $.getJSON('', {
 lat: position.coords.latitude,
 lng: position.coords.longitude,
 type: 'JSON'
 }, function(result) {
 alert('Country: ' + result.countryName + '\n' + 'Code: ' + result.countryCode);

Getting Browser Name and Version in JavaScript

<script type="text/javascript">
var nVer = navigator.appVersion;
 var nAgt = navigator.userAgent;
 var browserName = navigator.appName;
 var fullVersion = ''+parseFloat(navigator.appVersion);
 var majorVersion = parseInt(navigator.appVersion,10);
 var nameOffset,verOffset,ix;
// In Opera, the true version is after "Opera" or after "Version"
 if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
 browserName = "Opera";
 fullVersion = nAgt.substring(verOffset+6);
 if ((verOffset=nAgt.indexOf("Version"))!=-1)
 fullVersion = nAgt.substring(verOffset+8);
 // In MSIE, the true version is after "MSIE" in userAgent
 else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = nAgt.substring(verOffset+5);
 // In Chrome, the true version is after "Chrome"
 else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
 fullVersion = nAgt.substring(verOffset+7);
 // In Safari, the true version is after "Safari" or after "Version"
 else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "Safari";
 fullVersion = nAgt.substring(verOffset+7);
 if ((verOffset=nAgt.indexOf("Version"))!=-1)
 fullVersion = nAgt.substring(verOffset+8);
 // In Firefox, the true version is after "Firefox"
 else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
 fullVersion = nAgt.substring(verOffset+8);
 // In most other browsers, "name/version" is at the end of userAgent
 else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <
 (verOffset=nAgt.lastIndexOf('/')) )
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
 browserName = navigator.appName;
 // trim the fullVersion string at semicolon/space if present
 if ((ix=fullVersion.indexOf(";"))!=-1)
 if ((ix=fullVersion.indexOf(" "))!=-1)
majorVersion = parseInt(''+fullVersion,10);
 if (isNaN(majorVersion)) {
 fullVersion = ''+parseFloat(navigator.appVersion);
 majorVersion = parseInt(navigator.appVersion,10);
 +'Browser name = '+browserName+'<br>'
 +'Full version = '+fullVersion+'<br>'
 +'Major version = '+majorVersion+'<br>'
 +'navigator.appName = '+navigator.appName+'<br>'
 +'navigator.userAgent = '+navigator.userAgent+'<br>'