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!

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用