模拟登陆 急需输入验证码

WBOY
Release: 2016-06-13 11:59:11
Original
990 people have browsed it

模拟登陆 需要输入验证码
最近好奇,想做下知乎网的模拟登陆。但是遇到了需要输入验证码的问题

我多次尝试着在浏览器上进行登陆操作,掌握了一些规律。

如果需要输入验证码,在获取雁验证码的时候,它会下发 cookie,每次重新获取验证码吗,相应的cookie就改变一次

然后我用curl 模拟登陆的时候,已经将验证码的cookie传回去了,但是每次都是提示验证码错误。

它需要提交的参数最多就4个呀。像_xsrf可以不用提交,也可以通过

具体的代码实现和 这个链接差不多。用它的方法也是不行的
 http://www.cnblogs.com/echo-something/archive/2012/07/18/2597831.html

<br><?php <br />/**<br> * @author 追逐__something<br> * @version $id<br> */<br>define('SCRIPT_ROOT',dirname(__FILE__).'/');<br>$act = trim(isset($_REQUEST['act']) ? $_REQUEST['act'] : "");<br>switch($act)<br>{<br>    case 'login':<br>        // 获取验证码<br>        $code = trim($_REQUEST['code']);<br>        <br>        // $loginParams为curl模拟登录时post的参数<br>//         $loginParams['act'] = 'login';<br>        $loginParams['password'] = '123123';<br>        $loginParams['email'] = [email protected]';<br>        $loginParams['captcha'] = $code;<br>        <br>        // $cookieFile 为加载验证码时保存的cookie文件名 <br>        $cookieFile = SCRIPT_ROOT.'cookie.tmp';<br>        <br>        // $targetUrl curl 提交的目标地址<br>        $targetUrl = 'http://www.zhihu.com/login';<br>        <br>        // 参数重置<br>        $content = curlLogin($targetUrl, $cookieFile, $loginParams);<br>        <br>        $targetUrl = "http://www.zhihu.com";<br>        <br>        echo vget($targetUrl);<br>    break;<br>    case 'authcode':<br>        // Content-Type 验证码的图片类型<br>        header('Content-Type:image/png');<br>        showAuthcode('http://www.zhihu.com/captcha.gif');<br>        exit;<br>    break;<br>}<br><br>/**<br> * 模拟登录<br> * @param string $url 提交到的地址<br> * @param string $cookieFile 保存cookie的文件<br> * @param string $loginParams 提交时要post的参数<br> * @return string $content 返回的内容<br> */<br>function curlLogin($url, $cookieFile, $loginParams)<br>{<br>	$header = array(<br>			'Accept:*/*',<br>			'Accept-Encoding:gzip,deflate,sdch',<br>			'Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.6',<br>			'Cache-Control:no-cache',<br>			'Connection:keep-alive',<br>			'Content-Type:application/x-www-form-urlencoded; charset=UTF-8',<br>			'Host:www.zhihu.com',<br>			'Origin:http://www.zhihu.com',<br>			'Referer:http://www.zhihu.com',<br>			'X-Requested-With:XMLHttpRequest'<br>	);<br>	<br>	$curl = curl_init(); //启动一个curl会话<br>	curl_setopt($curl, CURLOPT_URL, $url);             //要访问的地址<br>	curl_setopt($curl, CURLOPT_HTTPHEADER, $header);         //设置HTTP头字段的数组<br>	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);           //对认证证书来源的检查<br>	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);           //从证书中检查SSL加密算法是否存在<br>	curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.22 Safari/537.36'); //模拟用户使用的浏览器<br>	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);           //使用自动跳转<br>	curl_setopt($curl, CURLOPT_AUTOREFERER, 1);              //自动设置Referer<br>	curl_setopt($curl, CURLOPT_POST, 1);                     //发送一个常规的Post请求<br>	 curl_setopt($curl,CURLOPT_POSTFIELDS, $loginParams); //提交查询信息<br>	curl_setopt($curl,CURLOPT_COOKIEFILE, $cookieFile); //同时发送Cookie<br>	curl_setopt($curl, CURLOPT_TIMEOUT, 30);                 //设置超时限制防止死循环<br>	$result = curl_exec($curl);                              //执行一个curl会话<br>	<br>	if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {<br>		$headSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);<br>	}<br>	<br>	$this->_cookies($result);<br>	<br>	curl_close($curl);   <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!