CURL和重定向

CURL and redirect

Hi.

I am using CURL to check some sites. I am having problems with some redirects. Not every time, just a few cases.

In this case, I have a 302 code, but the redirect URL is the same as the original URL.

Dump of CURL variables:

array(23) {
["url"]=> string(33) "http://www.apostasurpresa.gov.br/"
["content_type"]=> string(24) "text/html; charset=UTF-8"
["http_code"]=> int(302) 
["header_size"]=> int(180) 
["request_size"]=> int(92) 
["filetime"]=> int(-1) 
["ssl_verify_result"]=> int(0) 
["redirect_count"]=> int(0) 
["total_time"]=> float(0.037685) 
["namelookup_time"]=> float(0.000171) 
["connect_time"]=> float(0.018808) 
["pretransfer_time"]=> float(0.01882) 
["size_upload"]=> float(0) 
["size_download"]=> float(0) 
["speed_download"]=> float(0) 
["speed_upload"]=> float(0) 
["download_content_length"]=> float(0) 
["upload_content_length"]=> float(0) 
["starttransfer_time"]=> float(0.037521) 
["redirect_time"]=> float(0) 
["redirect_url"]=> string(33) "http://www.apostasurpresa.gov.br/"
["primary_ip"]=> string(15) "200.201.166.106" 
["certinfo"]=> array(0) { } } 

Curl page:

$ch = curl_init();      
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,60);
curl_setopt($ch, CURLOPT_COOKIESESSION  , 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST ,'GET');
//      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//      curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);  // true
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTREDIR, 6);// 3 => 6
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // false
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
$txt_pagina = curl_exec($ch); 
$info=curl_getinfo($ch);
curl_close($ch);

Thanks

OK, following up with an answer after the comments :)

I checked this with wget and it actually redirects to several places before landing on the portal page URL you provided above.

The original URL throws a 302, refers back to itself, then a 301. Then the URL it redirects to gives a lot of javascript, and the javascript then does a redirect.

So your code isn't working because it isn't parsing the javascript to redirect...

So... the solution. Why not just point to the portal page instead of dealing with multiple redirects? BTW the portal redirects if the URL doesn't end with a / so use http://loterias.caixa.gov.br/wps/portal/loterias/ as your URL.