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.
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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.
Eddie ShipmanAll-around developerCommented:
Here's a tutorial on building a REST API using PHP that I have used several times before. It is not difficult to understand and because I already were aware of .Net WebAPI2, fairly easy to build my PHP API. I know it's more than 2 yrs old but it does work. Make sure you are able to use the .htaccess file as well because it is what makes the whole thing run correctly.

http://www.tutorialsface.com/2016/02/simple-php-mysql-rest-api-sample-example-tutorial/

Since this operation would be a POST, you would include your JSON in your PostVars.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Thanks for help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.