我想使用curl自动登录网站。但该网站称,必须在浏览器中启用Cookie。我搜索了很多但找不到它。如果有人可以帮忙,
在网站帮助中搜索了很多,我发现该网站使用了两个cookie Moodlesession和sessionid。我不知道如何处理卷曲。请帮忙。
表格如下:
<form action="http://site_name/login/index.php" method="post" id="login">
<div class="loginform">
<div class="form-label"><label for="username">Username</label></div>
<div class="form-input">
<input type="text" name="username" id="username" size="15" value="">
</div>
<div class="clearer"><!-- --></div>
<div class="form-label"><label for="password">Password</label></div>
<div class="form-input">
<input type="password" name="password" id="password" size="15" value="">
<input type="submit" value="Login">
<input type="hidden" name="testcookies" value="1">
</div>
<div class="clearer"><!-- --></div>
</div>
</form>
我正在使用的卷曲脚本是:
<?php
$username="myusername";
$password="mypassword";
$url="login_url";
$cookie="cookie.txt";
$postdata="username=".$username."&password=".$password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, true);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$result = curl_exec ($ch);
echo $result;
curl_close($ch);
&GT;
答案 0 :(得分:1)
将此选项添加到cURL
配置:
curl_setopt($ch, CURLOPT_COOKIEFILE,'cookie.txt');
确保您有一个名为cookie.txt
的文件(例如将其写入/ tmp目录)并授予权限777(或使apache用户成为此文件的用户并授予权限644)
我查看answer
答案 1 :(得分:0)
也许您必须在postdata变量中输入输入&#39; testcookies&#39;。类似的东西:
$postdata="username=".$username."&password=".$password."&testcookies=1";