2013-03-08 45 views
0

我正在一個項目。所以我必須從下面提到的網站獲取驗證碼。用戶將提交驗證碼和他的登錄憑據,然後我必須提取數據(爲了用戶的利益)。但是在我提交驗證碼之後,只需登錄頁面出現,什麼都不會發生。檢索驗證碼和登錄使用捲曲php

我已經嘗試過這與多個網站,只是登錄頁面出現。我知道如何在登錄後獲取數據,但驗證碼確實是直接從捲曲給人一種頭痛

<?php 

$cookie="cookie.txt"; 
function open($url) 
{ 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2'); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_COOKIE, 1); 
    curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie); 
    curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt ($ch, CURLOPT_REFERER, $url); 
    $result = curl_exec($ch); 
    curl_close($ch); 

    return $result; 
} 

function between($string, $start, $end) 
{ 
    $out = explode($start, $string); 

    if(isset($out[1])) 
    { 
     $string = explode($end, $out[1]); 
     echo $string[0]; 
     return $string[0]; 
    } 

    return ''; 
} 

function get_captcha() 
{ 
    $url = 'https://academics.vit.ac.in/student/stud_login.asp'; 

    $open = open($url); 

    $code = between($open, '<img src='https://academics.vit.ac.in/student/captcha.asp', '">'); 

    // echo 'https://academics.vit.ac.in/student/captcha.asp' . $code; 

    return 'https://academics.vit.ac.in/student/captcha.asp' . $code; 
    //return ; 

} 


function rahul() 
{ 
    $capth=htmlspecialchars($_POST['code']); 

    echo $capth; 

    $username="xyz"; 
$password="abc"; 
$url="https://academics.vit.ac.in/student/stud_login.asp"; 
$cookie="cookie.txt"; 
$veri=$capth; 

$com="Login"; 

$postdata = "regno=".$username."&passwd=".$password."&vrfcd=".$veri."&submit=".$com; 

$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, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // <-- add this line 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result; 


$data = curl_exec($ch); 

} 




?> 


<html> 
<body> 
<form action="" method="post"> 
<img src="<?php echo get_captcha(); ?>" border="0" /><br /> 
<input type="text" name="code" value="<?= isset($_POST['code']) ? htmlspecialchars($_POST['code']) : '' ?>" /><br /> 
<input type="submit" name="submit" value="submit"/> 
</form> 

<?php 


if(isset($_POST['submit'])) { 
rahul(); 
} 
?> 
</body> 
</html> 

回答

3

在這種情況下,開放capcthca圖像和二進制代碼返回。

<?php 
$cookie='cookie.txt'; 
if(!file_exists($cookie)){ 
     $fh = fopen($cookie, "w"); 
     fwrite($fh, ""); 
     fclose($fh); 
} 
$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_URL, "https://academics.vit.ac.in/student/captcha.asp"); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_COOKIE,1);   
    curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie); 
    curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    $result = curl_exec($ch); 
curl_close($ch); 
$ImageCaptcha=base64_encode($result); 
?> 

觀看圖片使用Base64編碼

<?php echo '<img src="data:image/jpeg;base64,'.$ImageCaptcha.'">'; ?> 

我已經測試此代碼&這是爲我工作