dongtanghuan1885 2013-02-01 02:40
浏览 986

请求重定向,即使CURLOPT_FOLLOWLOCATION设置为FALSE

I'm new to PHP. I have searched StackOverflow and followed instructions from answers to similar questions. However, the instructions are not working for me.

This is the issue:

I'm trying to fetch HTML using PHP cURL for parsing. http://actas.rfef.es/actas/NPortada This is a publicly-accessible page. When I request the page through a browser's address bar, it works fine. However, if the request is through cURL, I'm redirected to http://actas.rfef.es/actas/NLogin (0 byte blank page). I thought useragent may be an issue and set curl's user agent value and followlocation to false,but still it redirects!

Here is my code:

$home="http://actas.rfef.es/actas/NPortada";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$home);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER, TRUE);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:18.0)Gecko/20100101 Firefox/18.0");
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch,CURLOPT_COOKIEJAR, "cookies.txt");
echo curl_exec($ch);
curl_close($ch);

Why does it redirect, even though followLocation is set to false? There are no meta refresh tags in that target page.

How can I avoid being redirected and get the html of the targeted page? What am I doing wrong?

I've been struggling with this problem for the last 3 days. Please will anyone help me with this? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doucheng9058 2014-09-23 01:44
    关注

    browser OK! but curl ( on php ) does not work! then see the below

    Always, check curl option on command line! beacuse it's so easy!

    1. curl < url to visit >

    2. see the request header on browser's deveopment tool!

    and...

    1. curl with browser's request header

      curl < url to visit > with

      -A "User-Agent" or

      -e "Referer" or

      --cookie "Cookie Key:value"

      etc...

    In your case,

    http://actas.rfef.es/actas/NPortada working good on browser, but not work on curl

    you can see the redirection by using curl -v http://actas.rfef.es/actas/NPortada

    and see the browser and using development tool.... then no redirection founded!

    enter image description here

    and re-try curl with a browser's request header! User-Agent not working, Referer not working too.

    then try with cookie option!

    curl -v --cookie "JSESSIONID=B2F73A51E07D624FB205A114B2CC5D19" "http://actas.rfef.es/actas/NPortada"
    

    I found the solution. http://actas.rfef.es/actas/NPortada must be requested with cookie!

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置