PHP - How to send background request ( HTTP SMS API )

Asked

Viewed 799 times

-1

I am making a form which as soon as it is sent, it should send an SMS thanking for the contact to the number in which it was informed in the form.

I have the following form:

<form action="processa.php" method="post">
    <div>
        <label for="nome">Nome:</label>
        <input type="text" id="nome" name="nome" />
    </div>
    <div>
        <label for="email">E-mail:</label>
        <input type="email" id="email" name="email" />
    </div>
    <div>
        <label for="telefone">Telefone:</label>
        <input type="tel" id="telefone" name="telefone" />
    </div>
    <div>
        <label for="msg">Mensagem:</label>
        <textarea id="msg" name="mensagem"></textarea>
    </div>

    <div class="button">
        <button type="submit">Enviar sua mensagem</button>
    </div>
</form>

File - processes.php

<?php
    $email              = addslashes($_POST['email']);
    $nome               = addslashes($_POST['nome']);
    $telefone           = addslashes($_POST['telefone']);
    $mensagem           = addslashes($_POST['mensagem']);

    $destinatario    = "[email protected]";
    $assunto         = "Nova solicitação de contato";
    $msg             = "
Você recebeu a seguinte solicitação de contato:
-------------------------------------
Nome: $nome
Email: $email
Telefone: $telefone
Mensagem: $mensagem  ";

    $headers         = "MIME-Version: 1.1\r\n";
    $headers        .= "Content-type: text/plain; charset=UTF-8\r\n";
    $headers        .= "From: NOME DO CONTATO <[email protected]>\r\n"; // remetente
    $headers        .= "Return-Path: [email protected]\r\n"; // return-path
    $envio           = mail($destinatario, $assunto, $msg, $headers);

    if($envio){
       header ("location: sucesso.html"); 
    }else{
     echo "A mensagem não pode ser enviada";
    } 
?>

The form when it is filled out, it sends me an email with the data that was entered, and then redirects to sucesso.html.

I would like when the person submits the form, to send an SMS to the person who completed the form.

HTTP API - Example of use

http://portal.gtisms.com:2000/gti/API/[email protected]&senha=teste&msg=Obrigado+Por+Entrar+Em+Contato&n=11984010101&id=0001

How to adapt the php script for him to make a request for the HTTP API sending an SMS to the number provided? The number ($telephone) shall be assigned to the parameter n=.
I would like an example =(

  • 1

    This API I never used, but I used this https://www.nexmo.com/. For this you can use Curl, http://codular.com/curl-with-php

  • 1

    I didn’t understand what the doubt would be, you can use the CURL as mentioned. If you want to reduce the response time you can use the exec with a CURL ignoring the answer, so the page will be processed without waiting for Curl. Anyway, the question was half vacant.

  • 1

    Try file_get_contents passing this url.

  • 1

    If you really need to dispatch the answer immediately and continue processing the script after that, read the vcampitelli response here at Stack Overflow: http://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response

  • Thanks friends, I’ll take a look to see if I can do something =/

1 answer

2

Meet Curl? Should stay like this:

$sms_user = '[email protected]';
$sms_msg = 'Obrigado Por Entrar Em Contato';
$sms_senha = 'senha';

$sms_telefone = preg_replace('#[^\d]#', '', $telefone); //Remove todos caracteres que não forem números

//Codifica para url os dados
$sms_user = urlencode($sms_user);
$sms_msg = urlencode($sms_msg);
$sms_senha = urlencode($sms_senha);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://portal.gtisms.com:2000/gti/API/send.aspx?user=' . $sms_user . '&senha=' . $sms_senha . '&msg=' . $sms_msg . '&n=' . $sms_telefone . '&id=0001');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);

$data = curl_exec($ch);

if($data === false) {
    echo 'Erro ao executar o CURL: ' . curl_error($ch);
} else {
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($httpcode !== 200) {
        echo 'Erro ao requisitar o servidor';
    }
}

curl_close($ch);

//Depurar a saida:

var_dump($data);

If you don’t have Curl enabled, see in this reply how to do /a/169474/3635

Note that I added var_dump, but when you’re in producing you must remove it.

  • I knew only by name Gui, thank you very much for the answer, I will test!!! = D =D =D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.