I am designing REST api using Slim framework. I am using database mySql. and i am designing this API in php.
I am trying to fetch data about student from my table.
I am trying like this:-
<?php
header('Content-type: application/json');
// Include the Slim library
require 'Slim/Slim.php';
// Instantiate the Slim class
$app = new Slim();
// Create a GET-based route
$app->get('/hello/:name', 'hello');
function hello($name)
{
// here is code to access detail of $name
echo $name
// how can i get detail if i have value of name=:kuntal not name=kuntal
}
// Ready the routes and run the application
$app->run();
?>
I am trying this function using this url:-
192.168.1.101/hello/:kuntal
i need to get value of name as kuntal but in function i am getting value of name as :kuntal so please tell me how can i remove this :(colon) before the name.
Is another way to do this.
Please give me your suggestion if you are aware about slim framework to make REST API.
Thank you in advance.
Try this url: 192.168.1.101/hello/kuntal
i think this will definitely work for your code.
Slim is a really great framework. Before I started using it I had zero experience with frameworks, REST, understanding HTTP... I'm still a noob but Slim makes it fun.
Answer 1:
$app->get('/hello/:name', function($name) use ($app) {
// Your mySQL code here
// Process that information into output JSON?
// echo json_encode($array);
});
Answer 2:
Might I add that you check out Idiorm/Paris for your database needs? In the same philosophy as Slim, less is more. This is what code might look like with Paris.
class Friend extends Model {}
$app->get('/hello/:name', function($name) use ($app) {
$friend = Model::factory('Friend')->find_many($name); // Paris: all rows with name
$friendAry = $friend->as_array('id', 'name');
$response = $app->response(); // Slim Response object at work
$response['Content-Type'] = 'application/json';
echo json_encode($friendAry); // Output
});
Although, question (and maybe someone who knows more about REST can answer). Is that uri really a good one? From what I understand about REST, we want to point people to resources. Nouns I guess. I'm not sure what Hello would really mean in REST context. Why not make the resource user or friend with an ID as a slug?
$app->get('/friend/:id', function($id) use ($app) {
// Returns the friend with unique id
$friend = Model::factory('Friend')->find_one($id); // Name is already part of obj
$friendAry = $friend->as_array('id', 'name');
echo json_encode($friendAry);
}
You can then process that information, package it with a hello greeting, whatever the client is expecting. You can pass extra information like name into the parameters like so.
http://search.twitter.com/search?q=potato&count=10
Hope this helps. Very cool stuff. And someone give me feedback to let me know if my thinking is on the right page. I'm still learning too.
enter code here
<?php
error_reporting('E_NOTICE ^ E_ALL');
include_once 'api_function.php';
$con = new DB_con();
function getCurrentURL()
{
$currentURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$currentURL .= $_SERVER["SERVER_NAME"];
if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
{
$currentURL .= ":".$_SERVER["SERVER_PORT"];
}
$currentURL .= $_SERVER["REQUEST_URI"];
return $currentURL;
}
if($_POST['action']!="")
{
$action = $_POST['action'];
}
else
{
header("Content-type: application/json; charset=iso-8859-1");
$inputdata = file_get_contents('php://input');
$data = json_decode($inputdata, TRUE);
$action = $data['action'];
}
/**********************select Detail API Starts here**********************/
if($action == 'select')
{
$insert = $con->select("table_name",$data);
$msg = array("result" => $insert);
header('content-type: application/json');
echo json_encode($msg,true);
}
/**********************delete Detail API Ends here************************/
/*2nd file api_function.php*/
error_reporting('E_NOTICE ^ E_ALL');
define('DB_SERVER', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'db_name');
class DB_con {
protected $conn;
function __construct() {
$this->conn = mysqli_connec`enter code here`t(DB_SERVER, DB_USER, DB_PASS, DB_NAME) or die(mysqli_connection_error($this->conn));
//mysql_select_db(DB_NAME, $conn);
}
/**********************select API Ends here**********************/
public function select($ssTableName, $asFields) {
if (strcmp($asFields['action'], "select") == 0) {
if ($asFields['id'] != '') {
$query_checklogin = mysqli_query($this->conn, "delete from $ssTableName where id='".$asFields['id']."'")or die(mysqli_error($this->conn));
if ($query_checklogin != '') {
$msg = array('msg' => 'succesfully', 'statuscode' => '201');
return $msg;
}
} else {
$deta = array();
$select = mysqli_query($this->conn, "SELECT * from $ssTableName ")or die(mysqli_error($this->conn));
while($row = mysqli_fetch_assoc($select)){
array_push($deta,$row);
}
$msg = array('msg' => 'Record does not exist', 'statuscode' => '202', 'detail'=>$deta);
return $msg;
}
} else {
$msg = array('msg' => 'Something Error','statuscode' => '203');
return $msg;
}
}
/********************** API Ends here**********************/
}
?>