camafeu 0 Posted October 22, 2017 Report Share Posted October 22, 2017 Boas. Alguem sabe se existe forma de efectuar login no portal das finanças através do cURL? A ideia é fazer login e posteriormente fazer o get das faturas através do JSON (https://faturas.portaldasfinancas.gov.pt/json/obterDocumentosAdquirente.action). O código que tenho é o seguinte: //set the directory for the cookie using defined document root var $dir = "/home/conta/web/cookies"; //build a unique path with every request to store //the info per user with custom func. $path = $dir; $cookie_file_path = $path."/cookie.txt"; //login data $postData1 = array( 'path' => 'painelAdquirente.action', 'partID' => 'EFPF', 'authVersion' => '1', '_csrf' => '9555c9e9-a5a6-41a2-8241-e02aa4a6db1c', 'selectedAuthMethod' => 'N', 'username' => 'USER_NAME', 'password' => 'PASSWORD' ); $url = 'https://www.acesso.gov.pt/jsp/loginRedirectForm.jsp'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); //could be empty, but cause problems on some hosts curl_setopt($ch, CURLOPT_COOKIEFILE, $path); //could be empty, but cause problems on some hosts $answer = curl_exec($ch); if (curl_error($ch)) { echo curl_error($ch); } Obtenho o erro: Por motivos de ordem técnica não nos é possível responder ao seu pedido. Por favor tente mais tarde. Link to post Share on other sites
camafeu 0 Posted October 22, 2017 Author Report Share Posted October 22, 2017 Ja consigo fazer login corrigindo isto: curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData1)); No entanto, o portal das financas faz um redirect após login com sucesso. Já usei o parametro a baixo mas nada feito... não faz o redirect. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); Alguma ideia? Link to post Share on other sites
s0me0ne 0 Posted June 13, 2018 Report Share Posted June 13, 2018 Boas a todos, Dentro do mesmo assunto gostava de saber se alguém sabe como abrir o portal da at autenticado. Tenho o seguinte código: <form name="loginForm" target="_blank" id="loginForm" action="https://www.acesso.gov.pt/jsp/submissaoFormularioLogin" method="POST" style="" autocomplete="off" > <!-- TAB LOGIN consultarDocumentosAdquirente.action painelAdquirente.action--> <input type="hidden" name="path" value="painelAdquirente.action"> <input type="hidden" name="partID" value="EFPF"> <input type="hidden" name="authVersion" value="1"> <input type="hidden" name="_csrf" value="8ae60105-900e-4b71-af16-c7eeeda5c50b"> <input type="hidden" id="selectedAuthMethodNIF" name="selectedAuthMethod" value="N"> <input type="hidden" id="selectedAuthMethod" name="selectedAuthMethod" value="C"> <input type="hidden" name="username" id="username" value="XXXXXXXXXXXXXXXX"> <input type="hidden" class="password error" name="password" value="YYYYYYYYYYYYYYYY" id="password"> <button type="submit" name="sbmtLogin" id="sbmtLogin" tabindex="4" class="sa-btn1" value="Entrar"> Autenticar </button> </form> Está a dar-me erro no campo _csrf que pelo que verifiquei é um campo de ligação único. Se abrir o site https://www.acesso.gov.pt/jsp/loginRedirectForm.jsp?path=painelAdquirente.action&partID=EFPF e copiar o codigo para este formulário, consigo aceder uma única vez, se aceder uma segunda vez dá erro de Pedido Inválido. Já tentei fazer um parse ao html e retirar o código e registar um novo código csrf, mas obtenho sempre o mesmo erro. Alguma Ajuda? Link to post Share on other sites
camafeu 0 Posted June 13, 2018 Author Report Share Posted June 13, 2018 $cookie_file_path = $path."/cookie.txt"; $cookie_file_path2 = $path."/cookie2.txt"; $cookie_file_path3 = $path."/cookie3.txt"; //login data $postData1 = array( 'path' => 'painelAdquirente.action', 'partID' => 'EFPF', 'authVersion' => '1', '_csrf' => '4467-9222-41b7f3b72715', 'selectedAuthMethod' => 'N', 'username' => $nif, 'password' => $password ); //faturas data $postData2 = array( 'dataInicioFilter' => '2017-01-01', 'dataFimFilter' => '2017-10-21', 'ambitoAquisicaoFilter' => 'TODOS', '_' => '1611270412' ); $ch = curl_init(); // fetch cURL login $url = 'https://www.acesso.gov.pt/jsp/loginRedirectForm.jsp'; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); //could be empty, but cause problems on some hosts curl_setopt($ch, CURLOPT_COOKIEFILE, $path); //could be empty, but cause problems on some hosts curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, "https://faturas.portaldasfinancas.gov.pt/consultarDocumentosAdquirente.action"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData1)); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true ); $answer1 = curl_exec($ch); //parsing do html $html = $answer1; $crawler = new Crawler($html); $link = $crawler->filter('input[type="hidden"]'); $sign = $link->filter('input[name="sign"]')->attr('value'); $userID = $link->filter('input[name="userID"]')->attr('value'); $sessionID = $link->filter('input[name="sessionID"]')->attr('value'); $nif = $link->filter('input[name="nif"]')->attr('value'); $tc = $link->filter('input[name="tc"]')->attr('value'); $tv = $link->filter('input[name="tv"]')->attr('value'); $userName = $link->filter('input[name="userName"]')->attr('value'); $partID = $link->filter('input[name="partID"]')->attr('value'); //redirect data $postData3 = array( 'sign' => $sign, 'userID' => $userID, 'sessionID' => $sessionID, 'nif' => $nif, 'tc' => $tc, 'tv' => $tv, 'userName' => $userName, 'partID' => $partID ); // redirect login $url3 = "https://faturas.portaldasfinancas.gov.pt/painelAdquirente.action"; curl_setopt($ch, CURLOPT_URL, $url3); curl_setopt($ch, CURLOPT_REFERER, $url3); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData3)); $answer3 = curl_exec($ch); // fetch cURL faturas $url2 = "https://faturas.portaldasfinancas.gov.pt/json/obterDocumentosAdquirente.action"; curl_setopt($ch, CURLOPT_URL, $url2); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData2)); $answer2 = utf8_decode(curl_exec($ch)); curl_close($ch); $json_faturas = json_decode(html_entity_decode($answer2), true); Tens aqui um exemplo de como ir buscar as facturas do e-fatura com respectivo login permanente. Link to post Share on other sites
s0me0ne 0 Posted June 14, 2018 Report Share Posted June 14, 2018 @camafeu Obrigado pela resposta. Sabes dizer-me se o endereço do exemplo está a funcionar? Estou a testar e aparece tudo a branco, sem qualquer tipo de erro. Link to post Share on other sites
CarlosA 1 Posted July 16, 2018 Report Share Posted July 16, 2018 On 6/13/2018 at 5:48 PM, camafeu said: $cookie_file_path = $path."/cookie.txt"; $cookie_file_path2 = $path."/cookie2.txt"; $cookie_file_path3 = $path."/cookie3.txt"; //login data $postData1 = array( 'path' => 'painelAdquirente.action', 'partID' => 'EFPF', 'authVersion' => '1', '_csrf' => '4467-9222-41b7f3b72715', 'selectedAuthMethod' => 'N', 'username' => $nif, 'password' => $password ); //faturas data $postData2 = array( 'dataInicioFilter' => '2017-01-01', 'dataFimFilter' => '2017-10-21', 'ambitoAquisicaoFilter' => 'TODOS', '_' => '1611270412' ); $ch = curl_init(); // fetch cURL login $url = 'https://www.acesso.gov.pt/jsp/loginRedirectForm.jsp'; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); //could be empty, but cause problems on some hosts curl_setopt($ch, CURLOPT_COOKIEFILE, $path); //could be empty, but cause problems on some hosts curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, "https://faturas.portaldasfinancas.gov.pt/consultarDocumentosAdquirente.action"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData1)); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true ); $answer1 = curl_exec($ch); //parsing do html $html = $answer1; $crawler = new Crawler($html); $link = $crawler->filter('input[type="hidden"]'); $sign = $link->filter('input[name="sign"]')->attr('value'); $userID = $link->filter('input[name="userID"]')->attr('value'); $sessionID = $link->filter('input[name="sessionID"]')->attr('value'); $nif = $link->filter('input[name="nif"]')->attr('value'); $tc = $link->filter('input[name="tc"]')->attr('value'); $tv = $link->filter('input[name="tv"]')->attr('value'); $userName = $link->filter('input[name="userName"]')->attr('value'); $partID = $link->filter('input[name="partID"]')->attr('value'); //redirect data $postData3 = array( 'sign' => $sign, 'userID' => $userID, 'sessionID' => $sessionID, 'nif' => $nif, 'tc' => $tc, 'tv' => $tv, 'userName' => $userName, 'partID' => $partID ); // redirect login $url3 = "https://faturas.portaldasfinancas.gov.pt/painelAdquirente.action"; curl_setopt($ch, CURLOPT_URL, $url3); curl_setopt($ch, CURLOPT_REFERER, $url3); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData3)); $answer3 = curl_exec($ch); // fetch cURL faturas $url2 = "https://faturas.portaldasfinancas.gov.pt/json/obterDocumentosAdquirente.action"; curl_setopt($ch, CURLOPT_URL, $url2); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData2)); $answer2 = utf8_decode(curl_exec($ch)); curl_close($ch); $json_faturas = json_decode(html_entity_decode($answer2), true); Tens aqui um exemplo de como ir buscar as facturas do e-fatura com respectivo login permanente. @camafeu, como posso implementar este código diretamente sem instalar o PHP, Apache e afins? Será que é possível arranjar-se algo parecido em VBA excel? Obrigado Link to post Share on other sites
ccue2003 0 Posted March 29, 2019 Report Share Posted March 29, 2019 Boa noite Ainda consegue obter a informação com este script? Obrigado Link to post Share on other sites
pc.cesar 15 Posted May 6, 2019 Report Share Posted May 6, 2019 Alguém tem o código completo em PHP ou FOX e que possa disponibilizar para aceder ao e-fatura para importar as faturas dos fornecedores. Link to post Share on other sites
Thiago 1 Posted May 8, 2019 Report Share Posted May 8, 2019 Boas. Tenho um código funcional em C# que faz o download das faturas. Se houver interesse me avisem. Basicamente precisa fazer um GET e dois POSTS para conseguir logar e fazer o parse do HTML depois. Meu e-mail thiagov@me.com 1 Report Link to post Share on other sites
pc.cesar 15 Posted May 15, 2019 Report Share Posted May 15, 2019 Em 08/05/2019 às 12:47, Thiago disse: Boas. Tenho um código funcional em C# que faz o download das faturas. Se houver interesse me avisem. Basicamente precisa fazer um GET e dois POSTS para conseguir logar e fazer o parse do HTML depois. Meu e-mail thiagov@me.com Pode facultar o código completo, nao trabalho com C# mas seria mais facil fazer a equivalencia para PHC ou FOX Link to post Share on other sites
pc.cesar 15 Posted May 30, 2019 Report Share Posted May 30, 2019 Em 16/07/2018 às 17:48, CarlosA disse: @camafeu, como posso implementar este código diretamente sem instalar o PHP, Apache e afins? Será que é possível arranjar-se algo parecido em VBA excel? Obrigado Quais os passos para colocar o script a funcionar? pode facultar o codigo completo? Link to post Share on other sites
CarlosA 1 Posted May 30, 2019 Report Share Posted May 30, 2019 7 hours ago, pc.cesar said: Quais os passos para colocar o script a funcionar? pode facultar o codigo completo? Lamento, mas não tenho. Também gostava de conseguir fazê-lo, preferencialmente em excel VBA... Link to post Share on other sites
ftuc 3 Posted February 26, 2020 Report Share Posted February 26, 2020 É preciso pedir algum token? Link to post Share on other sites
pc.cesar 15 Posted June 28, 2020 Report Share Posted June 28, 2020 Boas, Conseguiste aceder ao efatura via PHP?, podes disponibilizar o codigo completo ? Link to post Share on other sites
Thiago 1 Posted June 28, 2020 Report Share Posted June 28, 2020 23 minutos atrás, pc.cesar disse: Boas, Conseguiste aceder ao efatura via PHP?, podes disponibilizar o codigo completo ? Boas , tenho o código em C#, pode ser transformado em api, pra consumir em qualquer linguagem, o código está no github, se desejar posso auxiliar no desenvolvimento, meu e-mail thiagov@me.com Link to post Share on other sites
pc.cesar 15 Posted June 28, 2020 Report Share Posted June 28, 2020 qual o endereço? Link to post Share on other sites
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now