WebService REST Simple

5

How to create a simple REST-type WebService that does HTTP communication from an Android device to the server using the PHP language?

My goal is to Requests and receive a Response from the server.

    
asked by anonymous 28.08.2015 / 19:10

1 answer

5

For php I suggest you use a framework called PHP Slim Framework , it's very easy to use, take a look:

link

Example usage:

<?php

require '../Slim/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->response()->header('Content-Type', 'application/json;charset=utf-8');

$app->get('/', function () {
echo "SlimProdutos ";
});

$app->get('/categorias','getCategorias');

$app->run();

function getConn()
{
return new PDO('mysql:host=localhost;dbname=SlimProdutos',
'root',
'',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);

}

function getCategorias()
{
$stmt = getConn()->query("SELECT * FROM Categorias");
$categorias = $stmt->fetchAll(PDO::FETCH_OBJ);
echo "{categorias:".json_encode($categorias)."}";
}

Accessing http://localhost/SlimProdutos/categorias already has the expected return.

link

    
28.08.2015 / 19:22