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

我是PHP的新手。 我搜索了StackOverflow,并按照类似问题的答案的说明。 但是,这些说明对我不起作用。</ p>

这就是问题所在:</ p>

我正在尝试使用PHP cURL获取HTML进行解析。

http://actas.rfef.es/actas/NPortada </ strong >
这是一个可公开访问的页面。 当我通过浏览器的地址栏请求页面时,它工作正常。 但是,如果请求是通过cURL,我会被重定向到 http://actas.rfef.es/actas / NLogin (0字节空白页)。
我认为useragent可能是一个问题,并将curl的用户代理值和 followlocation </ code>设置为false,但它仍然重定向!</ p>

这是我的代码:</ p>

  $ home =“http://actas.rfef.es/actas/NPortada”; 
$ ch = curl_init( );
; ncurl_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);

ncurl_close($ ch);
</ code> </ pre>

为什么它会重定向,即使 followLocation </ code>设置为 false </ code> ? 该目标页面中没有元刷新标记。</ p>

如何避免重定向并获取目标页面的html? 我做错了什么?</ p>

过去3天我一直在努力解决这个问题。 请有人帮我这个吗? 提前致谢。</ p>
</ div>

展开原文

原文

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.

dongxi4335
dongxi4335 可能重复如何使用cURL读取html正文内容
大约 6 年之前 回复
dongpo1846
dongpo1846 你有没有找到解决方案?
6 年多之前 回复
douze2890241475
douze2890241475 我得到了file_get_contents($url)的相同空白页面。可能是他们禁用了file_get_contents.I我正在寻找使用cURL的解决方案。
7 年多之前 回复
duanmao7553
duanmao7553 为什么不只使用file_get_contents?
7 年多之前 回复

1个回答

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!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐