我正在尝试使用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()对于你发送的每一个参数
这是因为你先在这里获取captcha,你再次提交它,你正在访问https://www.irctc.co.in/eticketing/loginHome.jsf页面,所以它再次为你生成新的captcha,但你已经填写了旧的captcha,所以不匹配新的captcha与旧的captcha明白了吗? ?