Performs function x minutes after submitting form

4

Good afternoon, good people, there was a problem to solve on a site, something that had not yet moved.

It is as follows, on the page I have a form where the person can register. By registering, I'll send you an email confirming this registration.

Now, let's start using SMS too, that is, the person registers, receives an email and an SMS confirming (company rules). But my manager wants the SMS to arrive after about 5 min that the person has registered on the site.

At the moment, I'm sending the SMS right away, but he wants it done the way he said it.

Can anyone help me?

Note: I submit the form on the same page as the insert.

function sendSms(){

            $nome = explode(" ",$_POST['txtNome']); // pega o nome via post , e joga cada palavra em um array

            $corrigir = array(' ','-','(',')','{','}','/','  ');// array que elimina caractres do telefone ,deixando somente os numeros 

            $corpoSMS = 'SKY HDTV:Caro(a).'$nome[0].',informamos que seus dados foram enviados para nosso dpt de cadastros.Dentro de 24h,entraremos em contato com maiores informacoes'; //monta o corpo do sms , mantendo o limite de 160 caracteres.

            $tel = str_replace($corrir,'',$_POST['txtCel']);

            $msg = str_replace(' ','+',$corpoSMS);

            //código da página enviapost.php
                $dados_post = array(
                        "" => $tel,
                        "?message=" => $msg,
                        );

                $context = stream_context_create(array(
                    'http' => array(
                    'method' => 'GET',
                    'header' => array(
                    'Content-Type: application/x-www-form-urlencoded'),
                    'content' => http_build_query($dados_post)
                    )
                )
            );

        $host = 'https://meuservidordesms.com.br/'.$tel.'?message='.$msg;
            $result = file_get_contents($host,false,$context);

            if(preg_match("/SUCESSO/", $result)){
                    echo '  <script type="text/javascript">
                                alert("SMS enviado com sucesso!");
                            </script>';
                        } else {
                            echo '<script type="text/javascript">alert("Erro enviar o SMS.<br>");</script>';
                    }

        }// FUNCAO QUE ENVIA O SMS AO CLIENTE.
    
asked by anonymous 13.07.2015 / 22:54

1 answer

2

As Israel said, it may be best to have a service running on the server, checking the new records in the database and sending the SMS at the scheduled time.

Anyway, I was interested in your question and did a little research. It looks like you can contact a child from the script that is accessed by the client using socket . This script responds quickly to "parent script" that then disconnects - from there in background .

Here's the "parent script":

<?php

echo "Teste de Socket para Script Filho";

// Data para diferenciar o momento do acesso ao da criação do arquivo .txt
date_default_timezone_set('America/Sao_Paulo');
$current_date = date('d/m/Y == H:i:s');

// Criando a conexao socket
$socketParaFilho = fsockopen("www.example.com", 80);

// Criando pacote HTTP
$paraScriptFilho = "POST /script.php?&param=value HTTP/1.0\n";
$paraScriptFilho .= "Host: www.example.com\n";

// A mensagem que sera enviada ao script filho
$dados = "Script Pai: pagina acessada em " . $current_date;
$paraScriptFilho .= "Content-Length: ".strlen($dados)."\n\n";

// Incluindo mensagem com a data em que a pagina foi visitada
$paraScriptFilho .= $dados;

// enviando pacote
fwrite($socketParaFilho, $paraScriptFilho);

// Esperando resposta do script "filho"
$resposta = fread($socketParaFilho, $dataSize);

// Fechando conexao
fclose($socketParaFilho);

?>

Now the "script child", note that it should only be called "script.php":

<?php

date_default_timezone_set('America/Sao_Paulo');
$post = file_get_contents('php://input');

// Aqui que acontece a "mágica"
ob_start();
ignore_user_abort(1);
set_time_limit(0);

$resposta = "OK";
echo $resposta;

ob_flush();

// A partir deste ponto, o "script pai" desconecta, e o script filho
// se mantêm em execução

sleep(60);
$arquivo = 'teste.txt';
$data = date('d/m/Y == H:i:s');
$texto = $post . "\r\nScript Filho: texto salvo em " . $data;
file_put_contents($arquivo, $texto);

?>

Once you've accessed the "parent script" , you can even close the browser window. 60 seconds later, a .txt document will be created on the server, showing the date the page was accessed and the date that the child saved the document.

Here is the result of my test:

Script Pai: pagina acessada em 20/07/2015 == 05:00:03
Script Filho: texto salvo em 20/07/2015 == 05:01:03

I found this example here .

PS. I do not understand PHP. I saw this example and thought I could help with your question.

    
20.07.2015 / 10:34