CURL PHP Login automatico

Asked

Viewed 579 times

1

I am upgrading an application I made some time ago, I try to log in via CURL, but I can’t make the application work with the cookie collected during the login via CURL. I can save the cookie but not use it. I don’t know if I’m missing the middle of logging in or how to use the cookie. NOTE: The application already works, if I log in to the site www.comprasnet.Gov.br and after logging F12 and get the cookie via Chrome. Apparently it generates a primary cookie when logging into https://www.comprasnet.gov.br/seguro/loginPortalFornecedor.asp, it redirects to the page https://www.comprasnet.gov.br/intro.htm generating the first cookie. After clicking on the menu that leads to the link https://www.comprasnet.gov.br/pregao/fornec/Acompanhar.asp, it generates the two cookies that are used.

Code that logs in:

<?php
$cookieold = __DIR__.DIRECTORY_SEPARATOR.'cookieJar.txt';
$cookieactive = __DIR__.DIRECTORY_SEPARATOR.'cookieactive.txt';
$ch = curl_init();
$url = "https://www.comprasnet.gov.br/seguro/loginPortalFornecedor.asp";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0');
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "perfil=Fornecedor&txtLogin=seulogin&txtSenha=suasenha&ambiente=Produção");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieold);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$output = curl_exec($ch);

// Define uma nova URL para ser chamada (após o login)
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0');
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieold);
curl_setopt($ch, CURLOPT_URL, 'https://www.comprasnet.gov.br/intro.htm');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Executa a segunda requisição
$content = curl_exec ($ch);

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0');
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieold);
curl_setopt($ch, CURLOPT_URL, 'https://www.comprasnet.gov.br/pregao/fornec/Acompanhar.asp');
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieactive);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Executa a terceira requisição
$content2 = curl_exec ($ch);

    if ($output === FALSE) {               // verifica erros no curl
       echo "cURL Error: " . curl_error($ch);
    }

curl_close($ch);
//echo $content2;
?>

Code I currently use to use the manually collected cookie:

// iniciar sessao
$ch = curl_init();

// opcoes
curl_setopt($ch, CURLOPT_URL, "https://www.comprasnet.gov.br/pregao/fornec/Acompanhar.asp");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0');
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, 'ASPSESSIONIDQGAADDTC=DDLGMJABKGCABIKENDGOBLJJ;ASPSESSIONIDQGCADASC=LFFGLJABPEAKKIOLNLLCOFIC');
//executar sessao
$output = curl_exec($ch);
    if ($output === FALSE) {               // verifica erros no curl
       echo "cURL Error: " . curl_error($ch);
    }
$output = strtr($output, $conversao); // função de conversão acentuado/não acentuado
//fechar sessao
curl_close($ch);

Any light on how to make it work? I’m not a programmer, I’m a builder who likes to venture =).

No answers

Browser other questions tagged

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