-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNameAPI.php
More file actions
66 lines (59 loc) · 1.98 KB
/
NameAPI.php
File metadata and controls
66 lines (59 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
require_once('PeopleDB.php');
class NameAPI
{
public function API()
{
header('Content-Type: application/JSON');
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET'://consulta
$this->getName();
break;
default://metodo NO soportado
echo 'METODO NO SOPORTADO';
break;
}
}
/**
* Respuesta al cliente
* @param int $code Codigo de respuesta HTTP
* @param String $status indica el estado de la respuesta puede ser "success" o "error"
* @param String $message Descripcion de lo ocurrido
*/
function response($code=200, $status="", $message="")
{
http_response_code($code);
if( !empty($status) && !empty($message) )
{
$response = array("status" => $status ,"message"=>$message);
echo json_encode($response,JSON_PRETTY_PRINT);
}
}
/**
* función que segun el valor de "action" e "id":
* - mostrara una array con todos los registros de personas
* - mostrara un solo registro
* - mostrara un array vacio
*/
function getName()
{
if($_GET['action']=='names')
{
$db = new PeopleDB();
if(isset($_GET['id']))
{
//muestra 1 solo registro si es que existiera ID
$response = $db->getPeople($_GET['id']);
echo json_encode($response,JSON_PRETTY_PRINT);
}else
{ //muestra todos los registros
$response = $db->getName();
echo json_encode($response,JSON_PRETTY_PRINT);
}
}else{
$this->response(400);
}
}
}//end class
?>