麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PHP > 正文

PHP curl模擬登錄帶驗證碼的網站

2024-05-04 23:40:38
字體:
來源:轉載
供稿:網友
最近接了個項目,其中有需求是要登錄帶驗證碼的網站,獲取數據,但是我們不可能人為的一直去記錄數據,想通過自動采集的方式進行,下面小編給大家帶來的相關代碼,對php curl 模擬登錄帶驗證碼的網站感興趣的朋友一起學習吧
 

需求是這樣的,需要登錄帶驗證碼的網站,獲取數據,但是不可能人為一直去記錄數據,想通過自動采集的方式進行,如下是試驗出來的結果代碼!有需要的可以參考下!

<?phpnamespace 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();  }  public function _initialize(){    foreach($this->cookieName as $key => $name)    {      $this->cookiePathFile[] = ROOT_PATH . $this->cookiePath . $this->cookieName[$key] . '_xxx.txt';    }  }  /**   * 登錄xxx   */  public function xxxLogin()  {    $username = I('username');    $password = I('password');    $verifyCode = I('verify');    $loginData = array(      '__VIEWSTATE' => '/wEPDwUKMTU0MzAzOTU4NmQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFDExvZ2luX1N1Ym1pdL/yae69NsY163G3yuP0lxjz8oXu',              //不把參數補全可能會不被響應哦      '__VIEWSTATEGENERATOR' => 'DC42DE27',      'txt_UserName' => $username,      'txt_PWD' => $password,      'txt_VerifyCode' => $verifyCode,      'SMONEY' => 'ABC',      'Login_Submit.x' => '52',      'Login_Submit.y' => '19',    );    $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 = "您好,歡迎來P,請先登錄。";        switch ($paramsFull['parsedQuery']['Warn'])        {          case '2':            $msg = '您輸入的驗證碼錯誤,請重試';            break;          case '3':            $msg = '該帳號不存在,還沒帳號?';            break;          case '5':            $msg = '賬戶已注銷';            break;          case '6':            $msg = '密碼錯誤,如果連續錯誤3次半小時內不能登錄!';            break;          case '20':            $msg = '今日密碼錯誤3次及以上,請于半小時后再來登錄!';            break;          case '21':            $msg = '今日您所在IP的所有帳號密碼錯誤9次以上,請于半小時后再來登錄!';            break;          case '22':            $msg = '登錄失敗,您所在IP今日登錄的帳號過多!';            break;          case '23':            $msg = '登錄失敗,驗證碼失效!';            break;          case '32':            $msg = '該帳號已經綁定其他xx帳號!';            break;          case '33':            $msg = '一臺電腦一天只能注冊一個帳號!';            break;        }        $this->error($msg, '', 5);      }else{        $_SESSION['user_id'] = '123456';      //登錄設置session        $this->success('登錄P網站成功', U('Index/index'), 5);      }    }  }  /**   * 獲取驗證碼   */  public function getVerifyCode()  {    $img = $this->_cookieRequest('http://xxx.com/VerifyCode_Login.aspx?id=' . rand(10000,999999), null, true, 1);    echo $img;  }  /**   * 刪除cookie   */  public function clearCookie()  {    for($i = 0; $i <count($this->cookieName); $i++)    {      setcookie($this->cookieName[$i], '', time() - 3600);    }//    unlink($this->cookiePathFile);    $this->success('清除cookie成功!');  }  /**   * 帶COOKIE的訪問curl   * @param $url 訪問地址   * @param bool|array $data 傳遞的數據   * @param bool $redirect 是否獲取重定向的地址   * @return mixed 地址或者返回內容   */  public function _cookieRequest($url, $data = null, $redirect = false, $cookieNum = 0)  {    $ch = curl_init();    $params[CURLOPT_URL] = $url;             //請求url地址    $params[CURLOPT_HEADER] = false;           //是否返回響應頭信息    $params[CURLOPT_RETURNTRANSFER] = true;       //是否將結果返回    $params[CURLOPT_FOLLOWLOCATION] = true;       //是否重定向    $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);    }    //判斷是否有cookie,有的話直接使用    if (!empty($_COOKIE[$this->cookieName[$cookieNum]]) && is_file($this->cookiePathFile[$cookieNum]))    {      $params[CURLOPT_COOKIEFILE] = $this->cookiePathFile[$cookieNum];   //這里判斷cookie    }    else    {//      $cookie_jar = tempnam($cookie_path, 'cookie');            //產生一個cookie文件      $params[CURLOPT_COOKIEJAR] = $this->cookiePathFile[$cookieNum];    //寫入cookie信息      setcookie($this->cookieName[$cookieNum], $this->cookiePathFile[$cookieNum], time() + 120);   //保存cookie路徑    }    curl_setopt_array($ch, $params);                      //傳入curl參數    $content = curl_exec($ch);    $headers = curl_getinfo($ch);//    echo $content;    curl_close($ch);    if ($url != $headers["url"] && $redirect == false)     {      return $headers["url"];     }      return $content;     }  }

登錄以后,就可以使用帶cookie的訪問其他頁面了!

ps:php curl 登錄淘寶

提交上去后顯示為填寫驗證碼,登錄不上去

 填寫驗證碼提交:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">  <head>       <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   <title></title>  </head>  <body>  <iframe id='img' src="b.php" width="950" height="300" scrolling="No" frameborder="0"></iframe>  <form action="tb.php" method="POST">  <textarea name="vv" cols="50" rows="10">umto=&action=Authenticator&event_submit_do_login=anything&from=tb&fc=default&style=default&css_style=&tid=XOR_1_000000000000000000000000000000_63504554470A7C717F750278&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=xxxx&need_check_code=&&TPL_checkcode=</textarea>  <input type="submit" />  </form>   </body></html>
<?phpsession_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); ?>

提取驗證碼

<?phpsession_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_63504554470A7C717F750278&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, $arr1); echo "<img src=".$arr1[1]." />";exit;?>


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 7777网站| 三级国产三级在线 | 91av网址 | 九九热视频免费在线观看 | 久久精品视频一区二区三区 | 在线播放中文 | 日韩区在线 | 日本黄色免费片 | chinese军人gay呻吟 | 毛片在线不卡 | 久久青草影院 | 久久国产精 | 久久激情国产 | 伊人在线视频 | av在线免费看网站 | 国产日韩久久久久69影院 | av老司机久久 | 精品国产一区二区三区四区在线 | www.91操| 亚洲爱爱图 | 免费黄色成人 | 午夜视频播放 | 欧美高清另类自拍视频在线看 | 久久国产一级片 | 91精品久久久久久久久网影视 | 久久久精品视频免费看 | 国产免费一区二区三区在线能观看 | 成人三级视频网站 | 成人毛片网 | 一级做人爱c黑人影片 | 美国一级黄色毛片 | 一级毛片免费大片 | 欧美一级免费在线观看 | 黄网站在线免费 | 久久伊人国产精品 | 国产www免费 | 国产一级毛片高清 | 欧美不卡 | 91久久久久久久一区二区 | 久久精品国产99国产精品亚洲 | 久久成年网站 |