使用curl登录时无法使用验证码

时间:2015-08-19 17:03:18

标签: php curl

  

我试图在使用CURL登录后获取我的主页,但我不知道   知道为什么不显示。我认为我的代码有问题;   请检查我的代码并告诉我这段代码中的错误

我尝试了所有的东西,但每次失败但我看到有可能登录这个网站,我有一个成功登录的软件,但它是试用版,我想在PHP中构建相同的东西

<?php 
$file_contents=file_get_contents("https://www.irctc.co.in/eticketing/loginHome.jsf");
$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='ansari4all';
      $pass='ansari4all';
      $captcha=$_REQUEST['j_captcha'];
      $submit=$_REQUEST['submit'];
      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, 'https://www.irctc.co.in/eticketing/loginHome.jsf');
      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.irctc.co.in/eticketing/<?php echo $imagesrc;?>" />
    captcha:<input type="text" name="j_captcha" />
    <input id="loginbutton" type="submit" name="submit" value="Login"  />   
</form>

1 个答案:

答案 0 :(得分:0)

我无法使用浏览器登录该网站,每次都说

enter image description here

似乎是页面的错误。如果没有,您应该使用cURL获取图像URL和图像本身,并像使用POST请求一样使用会话。这对于一些验证码非常重要。