[英]Unable to display home page using curl after login
我尝试使用CURL
登录后获得主页,但是我没有
知道为什么它不显示。 我认为我的代码出了点问题;
您能否检查我的代码,然后告诉我它可能有什么问题?
<?php
$file_contents=file_get_contents("https://www.bcarocks.com/");
$dom = new DOMDocument();
@$dom->loadHTML($file_contents);
$xpath = new DomXpath($dom);
$imgs = $xpath->query('//*[@id="cimage"]');
foreach($imgs as $img)
{
$imagesrc=$img->getAttribute('src') . PHP_EOL;
}
?>
<?php
//initial request with login data
if(isset($_REQUEST['submit']))
{
$user='test';
$pass='test';
$captcha=$_REQUEST['j_captcha'];
$submit=$_REQUEST['submit'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.bcarocks.com/');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "j_username=$user&j_password=$pass&j_captcha=$captcha&submit=$submit");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
//another request preserving the session
curl_setopt($ch, CURLOPT_URL, 'https://www.irctc.co.in/eticketing/home');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
echo $answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
}
?>
<form method="post">
<img src="https://www.bcarocks.com/<?php echo $imagesrc;?>" />
captcha:<input type="text" name="j_captcha" />
<input id="loginbutton" type="submit" name="submit" value="Login" />
</form>
该页面是通过curl加载的,可能是您的登录详细信息不正确。 而且,您应该始终对要发送的每个参数使用urlencode()。
这是因为您首先在这里获取了验证码,然后又一次提交了验证码,因此您正在访问https://www.irctc.co.in/eticketing/loginHome.jsf页面,因此它会再次为您生成新的验证码,但是您已经填充了旧的验证码,所以新的验证码与旧的验证码不匹配???
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.