PHP - How to fill a field using Curl?

Asked

Viewed 1,139 times

1

What code do I use to fill in web page fields with cURL?
These are the fields:

Campo Nome:

<li>
    <label for="supporter_full_name">your name*</label>
    <input class="textbox" id="supporter_full_name" name="supporter[full_name]" size="30" type="text" />
</li>

Campo Email:

<li>
    <label for="supporter_email">email*</label>
    <input class="textbox" id="supporter_email" name="supporter[email]" size="30" type="text" />
</li>

Boot:

<input class="large blue awesome" id="verificar_button" name="commit" type="submit" value="Verificar">

I have a list with nome and email And I want to put them in one textarea and get them tested, and when the value is valid, she come back with the name and the email written on the front - válido, and when the value is invalid he come back with name and email written on the front - inválido. Thank you in advance.

  • 2

    I think you’re confusing things. URL is not meant to fill anything. It will fetch the information you need, in a file or on another server. The form can only be filled in by printing the variables together with PHP, or via Javascript. It depends on how you are building the page.

  • I am mounting the page in php, as I do ?

  • What do you mean by "fill in the fields"? You speak of a placeholder, an example text that will stay in the input until the person type? What you want is to validate the form? Be a little clearer and more direct.

  • What he has to do, as I understand it, is to send a post using Curl, because this in theory would "fill" the field and "test it", as he said.

2 answers

1

This is very simple.

First the file structure is, according to Voce, ("I have a list with name and email below each other"):

nome
[email protected]

Then loop the file:

// Abre o arquivo
$arquivo = file('arquivo.txt');

$linha = 0; //loop

while ($linha < count($arquivo)) {

    $nome = $arquivo[$linha]; // Primeira linha
    $email = $arquivo[$linha + 1]; // Segunda linha

    // Inicia o CURL
    $ch = curl_init();

    // Valores que deseja enviar.
    $valoresPost = [
        'supporter[full_name]' => $nome,
        'supporter[email]' => $email
    ];

    // Define as configurações do CURL:    
    curl_setopt_array($ch, [

       // Define o URL:
       CURLOPT_URL, 'http://exemplo.com',

       // Indica que é um POST:
       CURLOPT_POST => true,

       // Define a array que irá enviar (neste caso será multipart/form-data):
       CURLOPT_POSTFIELDS => $valoresPost,

       // Indica para receber os resultados:
       CURLOPT_RETURNTRANSFER => true

    ];

    // Faz a requisição (e obtem resposta):    
    $result = curl_exec($ch);

    // Obtem informações do CURL (como código HTTP):
    $info = curl_getinfo($ch);

    // Encerra o CURL:
    curl_close($ch);

    if ($info["http_code"] === 200) {

        // Sucesso

    }else{

        // Falha

    }

    $linha = $linha + 2;
}

0

Bruno, you first have to join the two in an array, so you know that always an odd number is an email and an even number is a name.

After that, you must validate the data on Ubmit and print whether the data is valid or not!

Probably validation you will either have to do while printing the array items or creating an item in the array to store their validation on Submit!

Browser other questions tagged

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