使用Curl获取验证码并发送数据并获取结果

时间:2014-02-21 08:39:12

标签: php database curl

所以很重要的是php。我想从网站上获取catpcha。然后我想在我的网页上显示它并发送一个小的帖子请求并获得结果。我所说的网站是http://www.bollywoodmotion.com/mobile-tracker-with-name.html

我想从该页面获取验证码到我的页面,并在我的页面中输入验证码,并从我的页面发送相同的内容。我在使用c#的软件中完成了这个。我也想要一个网络版,因为我不知道php。我尝试搜索和卷曲的任何方式对我来说都是最好的选择。所以有人可以帮我提供示例php代码。

请求由此完成。

http://www.bollywoodmotion.com/mobile-tracker-process.html

POST /mobile-tracker-process.html HTTP/1.1
Host: www.bollywoodmotion.com
User-Agent: Mozilla/5.0 (Windows NT 6.2; rv:26.0) Gecko/20100101 Firefox/26.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://www.bollywoodmotion.com/mobile-tracker-with-name.html
Cookie: __utma=164959532.607980600.1392265746.1392293744.1392969352.5; __utmz=164959532.1392265754.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); 
PHPSESSID=t37p2pqeclbmc2tfvd8tt18qs5; __utmb=164959532.2.9.1392969356013; __utmc=164959532
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 36

num=960xxxxxx&6_letters_code=hy5xjf
HTTP/1.1 200 OK
Date: Fri, 21 Feb 2014 07:56:11 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip
Set-Cookie: dle_user_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.bollywoodmotion.com; httponly
Set-Cookie: dle_password=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.bollywoodmotion.com; httponly
Set-Cookie: dle_hash=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.bollywoodmotion.com; httponly
Last-Modified: Mon, 16 Sep 2013 23:55:25 GMT
Keep-Alive: timeout=10, max=30
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

1 个答案:

答案 0 :(得分:9)

这是我在SO上的问题,我没有得到答案。但是,我现在有了答案。这是你想要的完美例子。我使用您网站也包含的网页源代码中的captcha-url检索验证码。使用between()从captcha-url中提取该图像。然后维护会话并再次发送请求。部分是您必须将数据提交到表单的URL,而不是页面。

<?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', '">');
        return 'https://academics.vit.ac.in/student/captcha.asp' . $code;

    }

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

        echo $capth;

        $username="xyz"; 
        $password="abc"; 
        $url=url of the form in which you want to submit your data; 
        $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>