Curl Script works on Windows but not on Linux

0

I have a problem with curl. When I use this code in apache installed on Windows 10, it always works. When I use it on Linux, both on Ubuntu Server 16.04 and on CentOS 7, it gives both problems, sometimes it works after several attempts (1 in 5 on average). What is missing in the Linux configuration? Something related to cookware storage? I'm almost installing Windows Server 2012.: (

<?php
print buscaPagina();
function buscaPagina()
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    //curl_setopt($ch,CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);

    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    //curl_setopt($ch, CURLOPT_HEADER, true);

    //curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH,true);
    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_URL, 'https://eproc1.tjto.jus.br/eprocV2_prod_1grau/externo_controlador.php?acao=entrar&alt=21');
    curl_setopt($ch, CURLOPT_POSTFIELDS,'selSistema=2&txtUsuario=nomedeusuario&pwdSenha=aquiasenha');
    $retorno = curl_exec($ch);
    curl_close($ch);
    return $retorno;
}
    
asked by anonymous 28.11.2017 / 19:26

1 answer

0

Probably the cookie.txt file can not be written, because in linux you need permissions to write files:

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

Try to create before and set the permissions like this:

if (!is_file('cookie.txt')) {
    file_put_contents('cookie.txt', ''); //cria o arquivo se não existir
}

chmod('cookie.txt', 0755); //muda as permissões

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

Of course I used the 0755 permission assuming that the Apache or PHP user is the same for www-data .

However I recommend that you put the data of this cookie.txt into a folder outside the www or public_html folder, since the data in cookies may be sensitive which may even cause session hijacking if someone else navigate your site such as http://site.com/cookie.txt it will have all data required if you are using some authentication.

    
28.11.2017 / 19:33