slim framework api examples


I have recently thinking of  developing  api in oops php.but I got massed up writing functionality which later got maser and maser.So I decided to move on slim framework to develop  api service.Slim framework is light weight i don’t know why they say that may be file size in kb.so I started working out trying GET and POST using slim framework .

After downloading the slim framework I just  executed index file and worked just fine in my localhost.later decide to Get value using slim framework and used the example code to get the value through url.

Example:
I just created a php file name slimtest.php and passed value through url like
http://localhost/slimrestapi/slimtest.php/username/test/password/123/country/United state/state/mexio

<?php

require 'Slim/Slim.php';
 \Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$app->get('/username/:name/password/:pwd/country/:country/state/:state', function ($name,$pwd,$country,$state) {
    $array_post[]=["did"=>"123123","didtype"=>"unmetered","country"=>"United State","state"=>"mexio"];
    $array[]=["username"=>$name,"password"=>$pwd,"country"=>$country,"state"=>$state,"data"=>$array_post];
    echo json_encode($array);
    //echo "Hello, $name,password,$pwd,country,$country,state,$state";
});

$app->run();
?>

After learning the GET and decided to move on POST value through slim framework.So created a file named slimpost.php and form.php to post value.

form.php

<form action="slimpost.php" method="POST">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form> 

slimpost.php
<?php
require 'Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();

$app->post('/', function () {
    echo json_encode($_POST);

});

$app->run();

Help me to learn slim framework to use it in secured and authenticated.

Advertisements
About

myself pramodh kumar yet another php developer from India and have worked on oops,procedural,yii framework,codeigniter,wordpress,joomla,api and more.

Tagged with: , , , , , , , , ,
Posted in slim framework

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow php tutorial on WordPress.com
categories
Calendar
March 2014
M T W T F S S
« Feb   Apr »
 12
3456789
10111213141516
17181920212223
24252627282930
31  
Follow me on Twitter
Blog Stats
  • 75,930 hits
%d bloggers like this: