douzhigan1687 2016-10-23 21:10 采纳率: 0%
浏览 121

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

  • 写回答

1条回答 默认 最新

  • dthswrp84966 2016-10-27 23:28
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献