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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝