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

Eduardo Fuerte
Eduardo Fuerte used Ask the Experts™
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:


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:

    define('DB_HOST'        , "");
    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 {

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

            $sql = "INSERT INTO dbo.tb_individuo
             $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.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

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.
All-around developer
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.

Since this operation would be a POST, you would include your JSON in your PostVars.
Eduardo FuerteDeveloper and Analyst


Thanks for help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial