Web Service utilizando Verbos no PHP

22 de julho de 2022 Off Por Marcelo Martins

Nesta longa jornada de aprendizado, mMais uma lição aprendida.

No exemplo a seguir, crio um web service, onde utilizo uma unica chamada para acionar todos os verbos do ws.


<?php
/*phpinfo();*/
/*Registra webservice para processamento de jobs*/
ini_set('display_errors', 'Off');
error_reporting(E_ALL);

include "connectdb.php";


function iif($cond, $arg_2, $arg_3)
{
    if($cond){
		return $arg_2;
	} else {
		return $arg_3;
	}
}

function filtro($cond){
	//$cond = str_replace('"',"'");
	return $cond;

}

$typereq = $_SERVER['REQUEST_METHOD'];
//echo $typereq;

if ($typereq==='POST')
{
	//echo "POST";
    // The request is using the POST method
	$data = json_decode(file_get_contents("php://input"));
	if($data){
		$localguid = $dbhandle->real_escape_string($data->guid);
		$idbperguntas = $dbhandle->real_escape_string($data->idbperguntas);
		//echo var_dump(json_decode($foo, true));
	} else {
		$localguid = $dbhandle->real_escape_string($_POST['guid']);
		$idbperguntas = $dbhandle->real_escape_string($_POST['idbperguntas']);
		//echo "3";
	}
	//echo $localguid;
	//echo $idbperguntas;
}

if ($typereq==='PUT')
{
	//echo "PUT";
    // The request is using the POST method
	$data = json_decode(file_get_contents("php://input"));
	if($data){
		$localguid = $dbhandle->real_escape_string($data->guid);
		$idhistorico = $dbhandle->real_escape_string($data->idhistorico);
		$pergunta = $dbhandle->real_escape_string($data->pergunta);
		//echo var_dump(json_decode($foo, true));
	} else {
		$localguid = $dbhandle->real_escape_string($_POST['guid']);
		$idhistorico = $dbhandle->real_escape_string($_POST['idhistorico']);
		$pergunta = $dbhandle->real_escape_string($_POST['pergunta']);
		//echo "3";
	}
	//echo $localguid;
	//echo $idhistorico;
	//echo $pergunta;
	if(($pergunta)&&($pergunta!=''))
	{
		$query = "insert into bperguntas (pergunta) values ('".$pergunta."');";
	}
	//echo $query;
	$rs = $dbhandle->query($query);
}

if ($typereq === 'GET') {
	//echo "GET";
    // The request is using the POST method
	$data = json_decode(file_get_contents("php://input"));
	if($data){
		$localguid = $dbhandle->real_escape_string($data->guid);
		$idbpergunta = $dbhandle->real_escape_string($data->idbpergunta);
	} else {
		$localguid = $dbhandle->real_escape_string($_GET['guid']);
		$idbpergunta = $dbhandle->real_escape_string($_GET['idbpergunta']);
	}
}

if ($typereq === 'DELETE')
{
	//echo $typereq;
	$data = json_decode(file_get_contents("php://input"));
	if($data){
		$localguid = $dbhandle->real_escape_string($data->guid);
		$idbperguntas = $dbhandle->real_escape_string($data->idbperguntas);
	} else {
		$localguid = $dbhandle->real_escape_string($_GET['guid']);
		$idbperguntas = $dbhandle->real_escape_string($_GET['idbperguntas']);
	}
	if ($idbperguntas)
	{
		//$localguid = $data->guid;
		//$idbpergunta = $data->idbpergunta;
		$query = "delete from bperguntas where idbperguntas = ".$idbperguntas.';';
		if($dbhandle->query($query)==TRUE)
		{
			echo ($query);
			$strJSON =  '{"rs":[';
			$strJSON = $strJSON . '{';
			$strJSON = $strJSON . '"IDPergutas":'.$idbperguntas.',';
			$strJSON = $strJSON . '"resposta":"Registro excluido com sucesso!"';
			$strJSON = $strJSON . '}';
			$strJSON = $strJSON . ']}';
			echo($strJSON);
		}
		else
		{
			$strJSON =  '{"rs":[';
			$strJSON = $strJSON . '{';
			$strJSON = $strJSON . '"IDPergutas":'.$idbperguntas.',';
			$strJSON = $strJSON . '"resposta":"'.$dbhandle->error.'"';
			$strJSON = $strJSON . '}';
			$strJSON = $strJSON . ']}';
			echo($strJSON);

		}
	}
	else
	{
		$strJSON =  '{"rs":[';
	    $strJSON = $strJSON . '{';
		$strJSON = $strJSON . '"IDPergutas":'.$idbperguntas.',';
		$strJSON = $strJSON . '"resposta":"Registro não encontrado!"';
		$strJSON = $strJSON . '}';
		$strJSON = $strJSON . ']}';
		echo($strJSON);
	}
}

if($localguid!=GUID)
{
	//echo $localguid;
	//echo "  - - ";
	//echo GUID;
	$strJSON =  '{';
	$strJSON = $strJSON . '"mensagem":"Acesso negado"';
	$strJSON = $strJSON . '}';
	echo $strJSON;
	exit();
}


if (($typereq === 'GET')){
	if(($idbperguntas)&&($idbperguntas!='0'))
	{
		$query = "select idbperguntas, pergunta from bperguntas where (idbpergunta = ".$idbperguntas.");";
	} else {
		$query = "select idbperguntas, pergunta from bperguntas ;";
	}
	$rs = $dbhandle->query($query);

	$cont = 0;
	//echo $query;
	$strJSON =  '{"rs":[';
	//while($row=$rs->fetch_assoc())
	while($row=$rs->fetch_assoc())
	{

		if($cont!=0)
		{
			$strJSON = $strJSON . ',';
		}
		$strJSON = $strJSON . '{';
		$strJSON = $strJSON . '"idbperguntas":'.filtro(iif(($row['idbperguntas']!=''),$row['idbperguntas'],'[vazio]')).',';
		$strJSON = $strJSON . '"pergunta":"'.filtro(iif($row['pergunta']!='',$row['pergunta'],'[vazio]')).'"';
		$strJSON = $strJSON . '}';
		$cont ++;
	}
	$strJSON = $strJSON . ']}';
	if ($cont>0)
	{
		echo($strJSON);
	}
}
?>



Na chamada REQUEST_METHOD, captura qual o verbo do html associado a aquela requisição.

$typereq = $_SERVER[‘REQUEST_METHOD’];

Desta forma podemos criar ações especificas para determinados verbos.

if (($typereq === ‘GET’)||($typereq === ‘POST’)){