使用Curl获取验证码并发送数据并抓取结果 [英] Get captcha using Curl and send data and grab results

查看:107
本文介绍了使用Curl获取验证码并发送数据并抓取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想从该网页获取人机识别系统到我的网页,并在我的网页中输入验证码,并发送从我的页面。我做了这个在一个软件使用c#。我也想要一个web版本,因为我不知道php。任何方式,我试图搜索和卷曲是我最好的选择。所以可以有人帮助我的示例php代码。



这是由它执行的请求。



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



< pre class =lang-none prettyprint-override> 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
连接:keep-alive
Content-Type:application / x-www-form-urlencoded
Content-Length:36

num = 960xxxxxx& 6_letters_code = hy5xjf



  HTTP / 1.1 200 OK 
日期:Fri,21 Feb 2014 07:56:11 GMT
服务器:Apache
到期日:Thu,19 Nov 1981 08:52:00 GMT
Cache-控制: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,1970年1月1日00:00:01 GMT; path = /; domain = .bollywoodmotion.com; httponly
Set-Cookie:dle_password = deleted; expires = Thu,1970年1月1日00:00:01 GMT; path = /; domain = .bollywoodmotion.com; httponly
Set-Cookie:dle_hash = deleted; expires = Thu,1970年1月1日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
连接:Keep-Alive
Transfer-编码:chunked
Content-Type:text / html


解决方案

这是我对SO的问题,我没有得到答案。但是,我现在有答案。这是你想要的完美的例子。我使用您的网站还包含的网页的源代码中的验证码来检索验证码。使用之间的()从captcha-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;
}

($ 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;

}

函数rahul()
{
$ capth = htmlspecialchars($ _ POST ['code']);

echo $ capth;

$ username =xyz;
$ password =abc;
$ url =您要提交数据的表单的url;
$ 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 ,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); //< - 添加此行
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 =textname =codevalue =<?= isset($ _ POST ['code'])?htmlspecialchars($ _ POST ['code']):''? ; />< br />
< input type =submitname =submitvalue =submit/>
< / form>

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


So bascially am new to php. I want to get catpcha from a website. Then I wanna show it on my webpage and send a small post request and get the results. The site I am talking about is http://www.bollywoodmotion.com/mobile-tracker-with-name.html

I want to get the captcha from that page to my page and to enter captcha in my page and send the same as from my page. I had done this in a software using c#. I also want a web version, since I have no idea on php. Any way I tried searching and got curl is the best option for me. So can some one help me with the sample php code.

This the request doing by it.

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

解决方案

This was my question on SO for which i didn't get answer.However, i have the answer now.This is the perfect example that you want. I retrieved the captcha using the captcha-url in the source code of the webpage which your site also contains. Extract that image from captcha-url using between(). Then maintain the session and send the request again.The imp. part is that you have to submit the data to the URL of the form, not the page.

<?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>

这篇关于使用Curl获取验证码并发送数据并抓取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆