需求是這樣的,需要登入帶驗證碼的網站,擷取資料,但是不可能人為一直去記錄資料,想通過自動採集的方式進行,如下是實驗出來的結果代碼!有需要的可以參考下!
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>/s', $getBack, $match)){ echo 'matched\r\n'; print_r($match); }else{ echo $getBack, '
'; $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 = '該帳號已經綁定其他PC蛋蛋帳號!'; 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 cookieName); $i++) { setcookie($this->cookieName[$i], '', time() - 3600); }// unlink($this->cookiePathFile); $this->success('清除cookie成功!'); } /** * 帶COOKIE的訪問curl * @param $url * @param null $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 ($content != $headers && $redirect == false){ return $headers["url"]; } return $content; }}
以上就介紹了php使用curl類比登入帶驗證碼的網站,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。