php cannot copy remote file, but download it from the browser

Asked

Viewed 624 times

2

  • 1

    what error happens when you try in PHP?

  • What is the mistake? You need to tell us the symptoms to give the diagnosis

  • Oops. Normally the error returned is: Warning: copy(http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip): failed to open stream: Redirection limit reached, aborting in /home/megas028/public_html/test/update_results.php on line 43 failed to copy http://www1.caixa.gov.br/lotteries/lotteries/lotteries/D_megase.zip...

2 answers

4


Here’s what’s going on:

inserir a descrição da imagem aqui

As can be seen from the image, the server implements a cookie (security=true) and must redirect to the same url, if the cookie authorizes the download, otherwise it will be in a redirect loop until 'see' the cookie in the request. Guess that’s what was missing from your request. Try with curl the following:

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Cookie: security=true"));
curl_setopt($curl, CURLOPT_URL, 'http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip');
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
$return = curl_exec($curl);
curl_close($curl);
file_put_contents('file.zip', $return);

As you can see in this way we also sent the cookie, which was necessary for the server to authorize the download of the file.

Here’s the same request with file_get_contents:

$opts = array(
    'http' => array(
        'method'=>"GET",
        'header' => array(
            'Cookie: security=true'."\r\n",
            'User-Agent' => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1",
        )
    ),
);
$context = stream_context_create($opts);
file_put_contents('file.zip', file_get_contents('http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip', false, $context));
  • Solved! Thanks! Let me study this code right, I lost the morning in a lot of variety and nothing worked. Just this!

  • @Joãogurgel you preferred to use the curl. I’ve been wanting to find out why the other functions behave like this :\

  • @Wallacemaxters, I think I had to send the cookie with the request, user agent (I’m not sure about this last one but for yes by no longer send)

  • @Miguel, I’m guessing that’s what it is, too. It looks like the url redirects to itself.Maybe in the middle of the process there is a check for some missing header.

  • Exact @Wallacemaxters , should redirect to itself and only authorizes when see the cookie

  • 1

    Exact @Wallacemaxters as I suspected. Well try $ curl -vL http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip" ... Then it’s in a redirect loop.... Then do it again $ curl -vL http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip -b security=true (send the cookie with the request), here you already have access to the file

Show 1 more comment

0

The question is old, but the solution is new (10/12/2019):

$VContext = stream_context_create(["http" => ['method' => 'GET', 'header' => ['Cookie: security=true'."\r\n", 'User-Agent' => $_SERVER['HTTP_USER_AGENT']]]]);

$VMegaSena = "http://loterias.caixa.gov.br/wps/portal/loterias/landing/megasena/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOLNDH0MPAzcDbwMPI0sDBxNXAOMwrzCjA0sjIEKIoEKnN0dPUzMfQwMDEwsjAw8XZw8XMwtfQ0MPM2I02-AAzgaENIfrh-FqsQ9wNnUwNHfxcnSwBgIDUyhCvA5EawAjxsKckMjDDI9FQE-F4ca/dl5/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_HGK818G0KO6H80AU71KG7J0072/res/id=buscaResultado";

$VResultado = @file_get_contents($VMegaSena, false, $VContext);

if ($VResultado !== false)
   $VResultado = json_decode($VResultado);

var_dump($VResultado);

Browser other questions tagged

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