PHP - CURL Session finished?

4

I'm trying to create a theft to automate a process, as post here: Automate process - Robot?

Given the suggestions, I'm following using cURL. What is happening is the following: It manages to login, goes to the second page where the form is that I want the POST to be done but it does not return anything to me. My question is if I have to use some command that saves the login session. Because of what I'm seeing it is returning a link that displays User and invalid password or Invalid Session in the second POST. Here is my code:

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

// Define a URL original (do formulário de login)
curl_setopt($ch, CURLOPT_URL, 'http://site.com/consulta/index.php');

// Habilita o protocolo POST
curl_setopt ($ch, CURLOPT_POST, 1);
// Define os parâmetros que serão enviados
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'usuario=teste&senha=teste');
// Imita o comportamento patrão dos navegadores: manipular cookies
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
// Define o tipo de transferência (Padrão: 1)
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
// Executa a requisição
$store = curl_exec ($ch);

// Define uma nova URL para ser chamada (após o login)
curl_setopt($ch, CURLOPT_URL, 'http://site.com/consulta/forms.php');
// Habilita o protocolo POST
curl_setopt ($ch, CURLOPT_POST, 1);
// Define os parâmetros que serão enviados
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'campoNome=Joao');

// Executa a segunda requisição
$content = curl_exec ($ch);

//show information regarding the request
print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . 
                curl_error($ch);

// Encerra o cURL
curl_close ($ch);

If I put an echo of the $ content exits the form page, but without any information or typed fields ..

    
asked by anonymous 26.07.2015 / 00:04

1 answer

3

You do not reuse cookies and therefore do not have the correct answer. One tip is to create methods for each type of request, where they will reuse cookies:

function getRequest($url, $myCookieFile) {
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_COOKIEJAR => $myCookieFile,
        CURLOPT_COOKIEFILE => $myCookieFile
    ]);

    $response = curl_exec($ch);
    return $response;
}

function postRequest($url, $data, $myCookieFile) {
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_COOKIEJAR => $myCookieFile,
        CURLOPT_COOKIEFILE => $myCookieFile
    ]);

    $response = curl_exec($ch);
    return $response;
}

With the methods implemented above you can maintain session persistence between pages. An example usage would be as follows:

// Primeira requisição para postar data e ter minha sessão
postRequest('http://site.com/sessao-para-pegar-os-cookies', 'usuario=teste&senha=teste', 'cookies.txt');

// Segunda requisição com os cookies da primeira requisição
$resposta = getRequest('http://site.com/pagina-para-usuarios-autenticados', 'cookies.txt');
    
23.01.2017 / 22:59