php使用curl模拟登录带验证码的网站,curl验证码
php使用curl模拟登录带验证码的网站,curl验证码
需求是这样的,需要登录带验证码的网站,获取数据,但是不可能人为一直去记录数据,想通过自动采集的方式进行,如下是试验出来的结果代码!有需要的可以参考下!
<?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(); } 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的访问其他页面了!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

How to handle 301 redirection of web pages in PHPCurl? When using PHPCurl to send network requests, you will often encounter a 301 status code returned by the web page, indicating that the page has been permanently redirected. In order to handle this situation correctly, we need to add some specific options and processing logic to the Curl request. The following will introduce in detail how to handle 301 redirection of web pages in PHPCurl, and provide specific code examples. 301 redirect processing principle 301 redirect means that the server returns a 30

In Linux, curl is a very practical tool for transferring data to and from the server. It is a file transfer tool that uses URL rules to work under the command line; it supports file upload and download, and is a comprehensive transfer tool. . Curl provides a lot of very useful functions, including proxy access, user authentication, ftp upload and download, HTTP POST, SSL connection, cookie support, breakpoint resume and so on.

How to set cookies in php curl: 1. Create a PHP sample file; 2. Set cURL transmission options through the "curl_setopt" function; 3. Pass the cookie in CURL.

Curl in PHP is a powerful tool for communicating with different servers. In practical applications, 301 jumps are often encountered, that is, the server will redirect the request. This article will delve into the 301 jump mechanism in PHPCurl and provide specific code examples to help readers better understand and apply this function. What is a 301 jump? A 301 jump is a redirect instruction issued by the server, which means that the requested resource has been permanently moved to another location. When the browser or client sends a request
