Skip to content

Instantly share code, notes, and snippets.

@alganet
Created October 11, 2011 17:53
Show Gist options
  • Select an option

  • Save alganet/1278840 to your computer and use it in GitHub Desktop.

Select an option

Save alganet/1278840 to your computer and use it in GitHub Desktop.
<?php
/*
* This file belongs to PHP-MG.
* 2º PHP TALKS - Dojo
*
*/
use Respect\Relational\Mapper;
use Respect\Relational\Finder;
set_include_path(get_include_path().PATH_SEPARATOR.__DIR__.'/../library');
spl_autoload_register(require '../library/Respect/Loader.php');
$router = new \Respect\Rest\Router();
//Routes
$router->get('/', function()
{
header("Status: 400 Bad Request");
return null;
});
$router->get('/address/*', function($cep)
{
$pdo = new PDO('mysql:host=localhost;dbname=cidades_estados', 'development', '123456');
$pdo->exec("SET NAMES utf8");
$pdo->exec("SET CHARACTER SET utf8");
$stmt = $pdo->prepare("SELECT * FROM cep WHERE cep = ? ");
$stmt->execute(array($cep));
$cep = $stmt->fetch(PDO::FETCH_OBJ);
return $cep;
})->when(function($cep){
if(!preg_match('/^[0-9]{8}$/', $cep)) {
header("Status: 400 Bad Request");
return false;
}
})->accept(array(
'.json' => function($cep) {
header('Content-Type: application/json; charset=utf-8');
return json_encode($data);
},
'.xml' => function($cep) {
header('Content-Type: text/xml; charset=utf-8');
return '<endereco><estado>'.$cep->estado.'</estado><cidade>'.$cep->cidade.'</cidade><locadouro>'.$cep->locadouro.'</locadouro><bairro>'.$cep->bairro.'</bairro><tipo>'.$cep->tipo.'</tipo><observacao>'.$cep->observacao.'</observacao></endereco>';
},
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment