0

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

我需要一些 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);
4

2 回答 2

1

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

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

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

php手册中所述:

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. 
于 2010-05-20T15:38:39.647 回答
1

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

于 2010-05-20T15:40:44.603 回答