2010-05-20 47 views
0

是的,是的,我知道用户名和密码。如何从PHP身份验证背后的网站中大量获取页面

我需要一些技巧在PHP中登录到一个网站和检索一些图像/内容,像一个普通的网站。

很明显,使用卷曲file_get_contents它不起作用,因为我没有通过身份验证。

我该怎么办?

验证是正常的HTTP验证与POST。

编辑:好的谢谢你的帮助!

我张贴在这里工作的代码以供将来参考

//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); 
+0

您是否需要使用PHP以编程方式执行此操作。如果没有,请尝试使用wget或类似的东西。网站使用什么样的认证?一个HTML表单或HTTP基本认证,或更奇特的东西? – fmark 2010-05-20 15:39:10

回答

1

你可以卷曲认证。 Curl允许发送POST变量进行登录,并且还支持基本的HTTP身份验证。

+0

如果我这样做,我怎么能保持在会话身份验证成功和调用curl到其他页面? – apelliciari 2010-05-20 16:13:18

1

使用浏览器来验证自己的身份,出口饼干和通过卷曲使用它们。 在会话持续之前,您应该模拟您的用户。

我在赶时间,不能只是现在为您提供的代码,但是我觉得这个方向可以帮助你

可以使用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. 
+0

好提示! thanx – apelliciari 2010-05-21 08:08:19