Here’s what’s going on:
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));
what error happens when you try in PHP?
– Ricardo Moraleida
What is the mistake? You need to tell us the symptoms to give the diagnosis
– Wallace Maxters
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...
– João Gurgel