Curl script works on Windows, but not on Linux

Asked

Viewed 188 times

-1

I have a problem with Curl. When I use this code in apache installed on Windows 10, it always works. When I use on Linux, both on Ubuntu Server 16.04 and on Centos 7, in both gives problem, works sometimes, after several attempts (1 in 5 on average). What is missing in Linux configuration? Something related to Cookes 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;
}

1 answer

1


Probably the file cookie.txt cannot be written, because linux requires 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 permission 0755 assuming that the Apache or PHP user is the same for www-data.

However I recommend you to put the data of this cookie.txt in a folder outside the Past www or the folder public_html, because the data in cookies may be sensitive which may even lead to session hijacking, if someone navigates your site as for example http://site.com/cookie.txt it will have all necessary data if you are using some authentication.

Browser other questions tagged

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