php curl登录网站进行数据处理

时间:2012-03-06 15:56:08

标签: php curl

试试这个:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://site/takelogin.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=USER&password=PASS');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
curl_close ($ch);

所有内容都以 login.php 开头,但形成了 takelogin.php ,如果用户名和密码正确,则将其重定向到 acc.php 为了更好地理解发布 login.php html方面的一部分,我认为这很重要:

<form action="takelogin.php" id="signup" method="post">
<input id="username" name="username" type="text" maxlength="25" value="">
<input id="password" name="password" type="password" maxlength="60" value="">
<input name="commit" type="submit" value="Login">
<input type="hidden" name="returnto" value="/acc.php"

即使在浏览器网址栏中尝试输入: http://site/takelogin.php?returnto=acc.php&username=USER&password=PASS 没有运气。

我在卷曲剧本中缺少什么?

3 个答案:

答案 0 :(得分:0)

如果你想让curl遵循php重定向,你需要使用它:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

但是你的表单中还有“returnto”字段,你不会在你的curl脚本中使用它。你需要那个变量,还是可选的?

答案 1 :(得分:0)

如果您需要提供基本身份验证的凭据,请确保在表单收据脚本中添加以下内容。

curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

答案 2 :(得分:0)

默认情况下,Curl不会遵循重定向,因此有CURLOPT_FOLLOWLOCATION。

由于无法访问此登录系统,我们最多可以说登录脚本可能正在检查提交按钮的值,因此请将commit=Login添加到查询值中。