【问题标题】:How to mass-fetch pages from a website behind authentication in PHP如何在 PHP 中通过身份验证从网站批量获取页面
【发布时间】:2011-02-21 22:47:28
【问题描述】:

是的,是的,我知道用户名和密码。

我需要一些 php 技巧来登录网站并检索一些图像/内容,就像普通网站一样。

显然使用 curl o file_get_contents 它不起作用,因为我没有经过身份验证。

我该怎么办?

身份验证是使用 POST 进行的普通 HTTP 身份验证。

编辑:好的,感谢您的帮助!

我在这里发布工作代码以供将来参考

//login and set cookie
$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookiefile"); # SAME cookiefile
curl_setopt($curl, CURLOPT_URL, "url in which there is the login form"); 
curl_setopt($curl, CURLOPT_POSTFIELDS, "user=test&password=test&someparam=somevalue"); //put here the post/get values
$output = curl_exec($curl);

echo $output;

//finally fetch my content
curl_setopt($curl, CURLOPT_URL, $url_to_fetch); 
$output = curl_exec($curl);
echo $output;

curl_close ($curl);

【问题讨论】:

  • 您是否需要使用 PHP 以编程方式执行此操作。如果没有,请尝试使用 wget 等。网站使用什么样的身份验证? HTML 表单或 HTTP 基本身份验证,还是更奇特的东西?

标签: php http authentication curl fetch


【解决方案1】:

使用浏览器进行身份验证,导出 cookie 并通过 curl 使用它们。 在会话持续之前,您应该模拟您的用户。

我很着急,现在不能为你提供代码,但我认为这个方向可以帮助你

您可以使用 CURLOPT_COOKIEFILE 选项来指定存储 cookie 的文件。

php manual中所述:

The name of the file containing the cookie data. 
The cookie file can be in Netscape format, or just 
plain HTTP-style headers dumped into a file. 

【讨论】:

    【解决方案2】:

    您可以使用 curl 进行身份验证。 Curl 允许发送 POST 变量来登录,以及基本的 HTTP 身份验证。

    【讨论】:

    • 如果我这样做了,我怎样才能保持会话身份验证成功并调用 curl 到其他页面?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多