Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point what is needed to make a PHP page to works out like a Web-Service?

Hi Experts

Could you point what is needed to make a PHP page to works out like a Web-Service?

Actually the page runs at browser this way:

localhost/sqh/ws_insere_tb_individuo.php?cpf=9989898982&nome=Jurandir&apelido=apelido001&genero=1&data_nascimento=20000430&tel_celular=999111222&tel_fixo=444555666&email=teste@uol.com.br&CEP_residencia=06568140&Cuidador_Resp=Chico&senha=XXX&cliente_white_label=A

Open in new window


The page is perfectly running. No errors.

To make it runs as a Web-server I guess it first must to receive a json string, isn't it?

The page:
<?php

    define('DB_HOST'        , "xxx.xxx.x.xx");
    define('DB_USER'        , "xxxxx");
    define('DB_PASSWORD'    , "XXXXXXX");
    define('DB_NAME'        , "xxxxxxxx");
    define('DB_DRIVER'      , "sqlsrv");
    
    
    $pdoConfig  = DB_DRIVER . ":". "Server=" . DB_HOST . ";";
    $pdoConfig .= "Database=".DB_NAME.";";

    
    $cpf     = isset($_GET['cpf'])     ? $_GET['cpf']     : 'null';
    $nome    = isset($_GET['nome'])    ? $_GET['nome']     : 'null';
    $apelido    = isset($_GET['apelido'])    ? $_GET['apelido']     : 'null';
    $genero    = isset($_GET['genero'])    ? $_GET['genero']     : 'null';
    $data_nascimento    = isset($_GET['data_nascimento'])    ? $_GET['data_nascimento']     : 'null';
    $tel_celular    = isset($_GET['tel_celular'])    ? $_GET['tel_celular']     : 'null';
    $tel_fixo    = isset($_GET['tel_fixo'])    ? $_GET['tel_fixo']     : 'null'; 
    $email    = isset($_GET['email'])    ? $_GET['email']     : 'null';
    $CEP_residencia    = isset($_GET['CEP_residencia'])    ? $_GET['CEP_residencia']     : 'null';
    $Cuidador_Resp    = isset($_GET['Cuidador_Resp'])    ? $_GET['Cuidador_Resp']     : 'null';
    $senha    = isset($_GET['senha'])    ? $_GET['senha']     : 'null';
    $cliente_white_label  = isset($_GET['cliente_white_label'])    ? $_GET['cliente_white_label']     : 'null';
    
    
    try {
       if(!isset($connection)){
           

           $connection =  new PDO($pdoConfig, DB_USER, DB_PASSWORD);
           $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
           
       }
       

            $sql = "INSERT INTO dbo.tb_individuo
            (
              cpf
             ,nome
             ,apelido
             ,genero
             ,data_nascimento
             ,tel_celular
             ,tel_fixo
             ,email
             ,CEP_residencia
             ,Cuidador_Resp
             ,senha
             ,cliente_white_label
            )
            VALUES
            (
             '%s',
             '%s',
             '%s',
             '%s',
             '%s',
             '%s',
             '%s',
             '%s',
             '%s',
             '%s',
             '%s',
             '%s'
            );";
            
            
             $comando = sprintf($sql, $cpf, $nome, $apelido,$genero, $data_nascimento, $tel_celular, $tel_fixo, $email, 
                                $CEP_residencia, $Cuidador_Resp, $senha, $cliente_white_label);

       
           $query      = $connection ->query($comando);       
       
           $connection = null;
           

       } catch (PDOException $e) {
           $mensagem = "Drivers disponiveis: " . implode(",", PDO::getAvailableDrivers());
           $mensagem .= "\nErro: " . $e->getMessage();
           throw new Exception($mensagem);
       }
?>    

Open in new window


Could you give me any help to make this job done?

Thanks in advance.
Avatar of David Favor
David Favor
Flag of United States of America image

A few thing to keep in mind.

1) What you're talking about is writing an API service. Searching on terms like this or GitHub projects may help.

2) When calling API services best to secure them using an API key. I do this by setting up a 32 byte alphanumeric string + only allowing access to services using HTTPS calls.

Do your development on a public facing IP with an SSL cert.

This will greatly simplify your work.

3) This is a tricky one. Looks like your writing small data to a database.

The way I handle this is in 2x phases.

Phase #1: Incoming data is stored into a log file using rsyslog which accomplishes two primary aims.

First, data is serialized by rsyslog, so no file or database opens/closes which take a huge amount of transaction time.

Second, since data is logged by rsyslog, data will always be saved, so no worries about database outages.

Phase #2: Log rotation is done. Can be every few minutes, hours or once daily. After log rotates, the previous log is processed in background at a low priority (so API calls get highest priority) by code which walks a log file + makes appropriate database entries.

Note: You may think this is overkill until the day when you have a project succeed wildly + your API call frequency goes out the roof. When this occurs, you will be glad you've tooled your entire API so it scales forever, with near zero server load.
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eduardo Fuerte

ASKER

Thanks for help!