PHP Curl Analog Login with authentication code site _php instance

Source: Internet
Author: User

The

Requirement is this, need to log in with the authentication Code of the website, get the data, but it is impossible for people to record data, want to go through the way of automatic collection, the following is the result of the test code! There is a need to refer to the following!

<?php namespace Home\controller;
Use Think\controller;
  Class Logincontroller extends Controller {protected $cookieName = Array (' cookie_verify ', ' cookie_verify ');
  protected $cookiePath = '/cookie/';
  Protected $cookiePathFile = Array ();
  Public Function Index () {$this->display (); The Public Function _initialize () {foreach ($this->cookiename as $key => $name) {$this-&GT;COOKIEPATHF ile[] = Root_path. $this->cookiepath. $this->cookiename[$key].
    ' _xxx.txt ';
    }/** * Login XXX/Public function xxxlogin () {$username = I (' username ');
    $password = I (' password ');
    $verifyCode = I (' verify '); $loginData = Array (' __viewstate ' => '/wepdwukmtu0mzazotu4nmqyaquex19db250cm9sc1jlcxvpcmvqb3n0qmfja0tlev9ffgefde
      Xvz2lux1n1ym1pdl/yae69nsy163g3yup0lxjz8oxu ',//no parameter completion may not be answered oh ' __viewstategenerator ' => ' Dc42de27 ', ' Txt_username ' => $username, ' txt_pwd ' => $password, ' Txt_verIfycode ' => $verifyCode, ' Smoney ' => ' ABC ', ' login_submit.x ' => ', ' Login_submit.y ' => ' 1
    9 ',);
    $getBack = $this->_cookierequest (' http://xxx.com/noLogin.aspx ', $loginData); if (preg_match) ('/<div[^\<div]*?id\s*=\s*[\ ' \]{1}div_msg[\ ' \ ']{1}.*?> (. *?)
      <\/div>/s ', $getBack, $match)) {echo ' matched\r\n ';
    Print_r ($match);
      }else{echo $getBack, ' <br/> ';
      $paramsFull = Parse_url ($getBack);
      Parse_str ($paramsFull [' query '], $paramsFull [' parsedquery ']); if (!empty ($paramsFull [' parsedquery '] [' Warn '])} {$msg = "Hello, welcome to P, please login first."
        ";
            Switch ($paramsFull [' parsedquery '] [' Warn ']) {case ' 2 ': $msg = ' The authentication code you entered is wrong, please try again ';
          Break Case ' 3 ': $msg = ' This account does not exist, no account?
            ';
          Break
            Case ' 5 ': $msg = ' account cancelled ';
          Break Case ' 6 ': $msg = ' Password error, if consecutive error 3 times within half an hour can not log in!
            ';
     Break     Case ': $msg = ' Today password error 3 times and above, please login in half an hour!
            ';
          Break Case ': $msg = ' Today all your IP account password error more than 9 times, please login in half an hour!
            ';
          Break Case ': $msg = ' Login failed, your IP today login account too much!
            ';
          Break Case '% ': $msg = ' Login failed, verify code Invalid!
            ';
          Break Case ': $msg = ' The account has been bound to other XX account number!
            ';
          Break Case ': $msg = ' A computer can only register an account one day!
            ';
        Break
      $this->error ($msg, ', 5);      }else{$_session[' user_id ' = ' 123456 ';
      Login Settings Session $this->success (' Login p website success ', U (' Index/index '), 5); /** * Gets the Authenticode/public function Getverifycode () {$img = $this->_cookierequest (' http://xxx.c Om/verifycode_login.aspx?id= '.
    Rand (10000,999999), NULL, True, 1);
  Echo $img; /** * Delete Cookie */Public Function ClearCookie () {for ($i = 0; $i <count ($this->cookiename); $i ++) {Setcookie ($this->cookiename[$i], ", Time ()-3600);
    }//Unlink ($this->cookiepathfile); $this->success (' Erase cookie success!
  '); 
   /** * with cookie Access curl * @param $url access address * @param bool|array data passed by $data * @param bool $redirect whether to get the redirected address 
  * @return mixed address or return content */Public Function _cookierequest ($url, $data = null, $redirect = false, $cookieNum = 0)
    {$ch = Curl_init ();             $params [Curlopt_url] = $url;           Request URL address $params [curlopt_header] = false;       Whether to return the response header information $params [Curlopt_returntransfer] = true;       Whether to return the result $params [curlopt_followlocation] = true;
    Whether to redirect $params [curlopt_useragent] = ' mozilla/5.0 (Windows NT 5.1; rv:9.0.1) gecko/20100101 firefox/9.0.1 ';
      if ($data) {$params [curlopt_post] = true;
    $params [Curlopt_postfields] = Http_build_query ($data); //Determine if there is a COOKIE, and some words use if (!empty ($_cookie[$this->cookiename[$cookieNum]]) && is_file ($this-&Gt;cookiepathfile[$cookieNum])) {$params [curlopt_cookiefile] = $this->cookiepathfile[$cookieNum];            Here the cookie} else {//$cookie _jar = Tempnam ($cookie _path, ' cookie ') is judged;    Produces a cookie file $params [Curlopt_cookiejar] = $this->cookiepathfile[$cookieNum];   Write cookie Information Setcookie ($this->cookiename[$cookieNum], $this->cookiepathfile[$cookieNum], time () + 120);                      Save Cookie Path} curl_setopt_array ($ch, $params);
    Incoming Curl parameter $content = curl_exec ($ch);
$headers = Curl_getinfo ($ch);
    Echo $content;
    Curl_close ($ch);
     if ($url!= $headers ["url"] && $redirect = = False) {return $headers [url];
     return $content; }   }

After logging in, you can use a cookie to access other pages!

ps:php Curl Login Taobao

Submitted to the display after the verification code, login does not go up

Fill in the Verification code submitted:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  
<?php session_start ();
if (Empty ($_session[' Cookie_jar ')) exit ();
$cookie _jar=$_session[' Cookie_jar '];
$post _fields=$_post["VV"]; 
$ch = Curl_init (' https://login.taobao.com/member/login.jhtml '); 
curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1; trident/4.0; Qqwubi 133; Embedded Web Browser from:http://bsalsa.com/; SLCC2. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Tablet PC 2.0;. net4.0c;. net4.0e; infopath.3; 
Media Center PC 6.0) "); 
curl_setopt ($ch, Curlopt_returntransfer, 1); 
curl_setopt ($ch, Curlopt_post, 1); 
curl_setopt ($ch, Curlopt_postfields, $post _fields); 
curl_setopt ($ch, Curlopt_ssl_verifypeer, false); 
curl_setopt ($ch, Curlopt_ssl_verifyhost, 1); 
curl_setopt ($ch, Curlopt_cookiejar, $cookie _jar); 
$data = curl_exec ($ch);
Curl_close ($ch);
Echo $data, exit; 
$ch = Curl_init (' http://www.taobao.com '); curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1; trident/4.0; Qqwubi 133; Embedded Web Browser from:http://bsalsa.com/; SLCC2. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Tablet PC 2.0;. net4.0c;. net4.0e; infopath.3; 
Media Center PC 6.0) "); 
curl_setopt ($ch, Curlopt_header, 0); 
curl_setopt ($ch, Curlopt_returntransfer, 0); 
curl_setopt ($ch, Curlopt_cookiefile, $cookie _jar); 
curl_setopt ($ch, Curlopt_ssl_verifypeer, TRUE); 
Curl_exec ($ch); 
Curl_close ($ch); ?>

extract validation code

<?php session_start ();
$cookie _jar=tempnam ("./temp/", "Cookie");
$_session[' Cookie_jar ']= $cookie _jar; $post _fields = "action=authenticator&event_submit_do_login=anything&from=tb&fc=default&style= default&css_style=&tid=xor_1_000000000000000000000000000000_635045544 70A7C717F750278&support= 000001&ctrlversion=1,0,0,7&logintype=3&minititle=&minipara=&pstrong=&llnick=&sign= &need_sign=&isignore=&full_redirect=&popid=&callback=&guf=¬_duplite_str=&need_user _id=&poy=xor_1_000000000000000000000000000000_625a424a45137c6f7a7f0b786d08&gvfdcname=&gvfdcre= 
&from_encoding=&tpl_redirect_url=http:www.taobao.com&tpl_username=xxx&tpl_password=xxx "; 
$ch = Curl_init (' https://login.taobao.com/member/login.jhtml '); curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1; trident/4.0; Qqwubi 133; Embedded Web Browser from:http://bsalsa.com/; SLCC2. NET CLR 2.0.50727. NET CLR 3.5.30729;. NET CLR 3.0.30729; Tablet PC 2.0;. net4.0c;. net4.0e; infopath.3; 
Media Center PC 6.0) "); 
curl_setopt ($ch, Curlopt_header, 0); 
curl_setopt ($ch, Curlopt_returntransfer, 1); 
curl_setopt ($ch, Curlopt_ssl_verifypeer, false); 
curl_setopt ($ch, Curlopt_ssl_verifyhost, 2); 
$data = curl_exec ($ch); 
Curl_close ($ch); Preg_match ("/id=\" um_to\ "name=\" umto\ "value=\" (. *?) 
\ "\/>/", $data, $arr); $post _fields = "umto=". $arr [1]. "&". $post _fields. " 
&tpl_checkcode= "; echo "<textarea cols=50 rows=10>". $post _fields. 
"</textarea><br/>"; 
$ch = Curl_init (' https://login.taobao.com/member/login.jhtml '); 
curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1; trident/4.0; Qqwubi 133; Embedded Web Browser from:http://bsalsa.com/; SLCC2. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Tablet PC 2.0;. net4.0c;. net4.0e; infopath.3; 
Media Center PC 6.0) "); Curl_setopt ($ch, Curlopt_returntransfer, 1); 
curl_setopt ($ch, Curlopt_post, 1); 
curl_setopt ($ch, Curlopt_postfields, $post _fields); 
curl_setopt ($ch, Curlopt_ssl_verifypeer, false); 
curl_setopt ($ch, Curlopt_ssl_verifyhost, 2);
curl_setopt ($ch, Curlopt_cookiejar, $cookie _jar); 
curl_setopt ($ch, Curlopt_cookiefile, $cookie _jar); 
$data = curl_exec ($ch);
Curl_close ($ch); Preg_match ("/img id=\" j_standardcode_m\ "src=\" (. *?) 
\ "data-src=/", $data, $arr 1);
echo "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.