PHP - CURL Session ended?

Asked

Viewed 856 times

3

I’m trying to create a robot to automate a process, as post here: Automating process - Robot?

Given the suggestions, I am following using Curl. What is happening is the following: He can login, goes to the second page where has the form I want the POST to be done but it does not return anything. My question is whether I have to use a command that stores the login session. Because from what I am seeing it is returning a link that presents User and invalid password or Invalid session in the second POST. Follow 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 place a $content echo exits the form page, but without any information or fields typed..

  • I would need to run the script to give you an exact answer, do you know the Pastebin? is a repository where Voce can determine the maximum time that the file will become available, if possible create one with its complete code for me to run and point out its flaws..

  • @user3163662 The site and the user are not mine. I could not pass like this, but I understand that you could only see with the full. But at first, the requisition, the structure is right?

  • It seems to me that cookies are not going to the second page where you want to get the results, so you need to activate them. I did a search (I recommend to always do research in English, as it is easier to find answered questions) where the user wanted the same as you, see if it serves you: http://stackoverflow.com/questions/13241344/grabbing-data-from-a-website-with-curl-after-logging-in

1 answer

3

You do not reuse cookies and therefore do not have the correct answer. A tip is to create methods for each type of request, where these 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 above implemented methods you can keep session persistence between pages. An example of use would be the following:

// 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');

Browser other questions tagged

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