Webservice REST Simple

Asked

Viewed 3,559 times

5

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

My goal is to make Requests and receive as a result Response server.

  • post part of the error of your code, not to be negativated

  • No error as I have not implemented yet, I would like to know if there is something to follow, or if there is any Framework that can help me make this connection.

  • You run a PHP code on Android or create a server for Android connect?

1 answer

5

To php I suggest you use a framework called PHP Slim Framework, is very easy to use, give a look:

http://tableless.com.br/php-slim-framework/

Example of use:

<?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.

http://imasters.com.br/linguagens/php/aprenda-a-usar-o-restful-com-php-e-slim-framework

Browser other questions tagged

You are not signed in. Login or sign up in order to post.