简体   繁体   English

CURL PHP自动登录

[英]CURL PHP Automatic Login

Im trying to make an application that do a login via CURL, but i cant make this application to work, with the cookie saved after the login. 我正在尝试制作一个通过CURL进行登录的应用程序,但是我无法使该应用程序正常工作,并且在登录后保存了cookie。 Aparently i can save the cookie, but can use it. 显然我可以保存cookie,但是可以使用它。 I dont know if im doing wrong in the login code or the way to use the cookie saved. 我不知道我是否在登录代码或使用保存的cookie的方式上做错了。 This application already work if i get the cookie manually via Chrome after login. 如果我在登录后通过Chrome手动获取Cookie,则此应用程序已经可以使用。 I think it generate a primary cookie after the login at this page: https://www.comprasnet.gov.br/seguro/loginPortalFornecedor.asp , that redirects to this one https://www.comprasnet.gov.br/intro.htm , generating the first cookie. 我认为它在登录此页面后会生成一个主要cookie: https : //www.comprasnet.gov.br/seguro/loginPortalFornecedor.asp ,它将重定向到此https://www.comprasnet.gov.br/intro .htm ,生成第一个cookie。 After i hit the menu to this page: https://www.comprasnet.gov.br/pregao/fornec/Acompanhar.asp , it generates the two cookies needed to work. 在我点击此菜单的菜单后: https : //www.comprasnet.gov.br/pregao/fornec/Acompanhar.asp ,它将生成工作所需的两个cookie。

Code i made to try to do the login: 我尝试登录的代码:

<?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 that i already use with the cookie a get manually: 我已经与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 help will be aprecciate. 任何帮助将不胜感激。 Im not a programmer, just a curious guy trying to make my job easier. 我不是程序员,只是一个好奇的家伙,试图使我的工作更轻松。 This application im making id for personnal use only. 此应用程序仅使ID供个人使用。

my suggestion is to curl_close / curl_init after each output line. 我的建议是在每个输出行之后执行curl_close / curl_init You are carrying over settings from the previous curl session like curl_setopt($ch, CURLOPT_COOKIESESSION, true) and CURLOPT_POSTFIELDS 您正在继承上一个curl会话中的设置,例如curl_setopt($ch, CURLOPT_COOKIESESSION, true)CURLOPT_POSTFIELDS

so 所以

$output = curl_exec($ch);
curl_close($ch);
$ch = curl_init();

$content = curl_exec($ch);
curl_close($ch);
$ch = curl_init();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM