这段时间一直写个项目, 从切图到前端到后台都要搞定,真tm累。 今天下午手残,不停用错误的密码去模拟登录微信公众平台,结果后来出现验证码,瞬间悲剧(菜鸟从来没搞过带验证码的)。
研究了一下,发现其实很简单,直接把生成的二维码图片拉过来,让用户自己输入,再提交一遍表单即可。其实中间还是有些坑,比如生成验证码的时候,要把cookie保存下,登录的时候要带上这个cookie,要不然会总是提示需要验证码。
客户端:
1 <body> 2 <form> 3 <p><input type="text" name="user" value="" />帐号</p> 4 <p><input type="password" name="pass" value="" />密码</p> 5 <div style="display:none"> 6 <input type="text" name="code" />验证码 7 <img src="" id="imgcode" /><a href="javascript:;" id="changCode">换一张</a> 8 </div> 9 <input type="button" class="submit" value="绑定"/> 10 </form> 11 <script src="http://code.jquery.com/jquery-1.9.0.js"></script> 12 <script type="text/javascript"> 13 $(function(){ 14 $('.submit').on('click', function(){ 15 16 var user = $('input[name=user]').val(), 17 pass = $('input[name=pass]').val(); 18 19 var data = { 20 'user' : user, 21 'pass' : pass 22 }; 23 24 if ( $('img').attr('src') != '' ) { 25 var code = $('input[name=code]').val(); 26 data.code = code; 27 } 28 29 $.post('wx-login.php', data, function(res){ 30 31 var oRes = JSON.parse(res); 32 //console.log(oRes); 33 // -8 需要验证码 34 if ( oRes.base_resp.ret == '-8') { 35 $('img').attr('src','code.php?u='+user); 36 $('div').show(); 37 } else if( oRes.base_resp.ret == 0 ){ 38 alert('登录成功'); 39 } else { 40 alert('登录失败'); 41 } 42 }); 43 44 }); 45 46 $('#changCode').on('click', function(){ 47 var user = $('input[name=user]').val(); 48 $('img').attr('src','code.php?u='+user+'&s='+Math.random(1)); 49 }); 50 }); 51 </script> 52 </body>
服务端-验证码:
code.php
1 $username = $_GET['u']; 2 $url = "https://mp.weixin.qq.com/cgi-bin/verifycode?username={$username}&r=".time(); 3 4 //必须绝对路径 5 $file = dirname(__FILE__).'/cookie/cookie_'.$username.'.txt'; 6 7 $curl = curl_init(); 8 curl_setopt($curl, CURLOPT_URL, $url); 9 curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); 10 curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); 11 curl_setopt ($curl, CURLOPT_TIMEOUT, 10 ); 12 curl_setopt($curl, CURLOPT_HEADER, 0); 13 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
15 curl_setopt($curl, CURLOPT_COOKIEJAR, $file); 16 $res = curl_exec ($curl); 17 curl_close ( $curl ); 18 header("Content-type:image/jpeg"); 19 echo $res;
服务端-登录:
wx-login.php
1 $arr = array( 2 'username'=>$_POST['user'], //wx公众帐号 3 'pwd'=>md5($_POST['pass']), //wx公众帐号密码 4 'f'=>'json' 5 ); 6 7 if (isset($_POST['code'])) { 8 $arr['imgcode'] = $_POST['code']; 9 } 10 11 $file = dirname(__FILE__).'/cookie/cookie_'.$arr['username'].'.txt'; 12 13 $headers = array( 14 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36', 15 'Referer:https://mp.weixin.qq.com/', 16 ); 17 18 $curl = curl_init(); 19 curl_setopt($curl, CURLOPT_URL, 'https://mp.weixin.qq.com/cgi-bin/login'); 20 curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); 21 curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 ); 22 curl_setopt ($curl, CURLOPT_TIMEOUT, 10 ); 23 curl_setopt($curl, CURLOPT_HEADER, 0); 24 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);26 curl_setopt($curl, CURLOPT_POST, 1); 27 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($arr)); 28 curl_setopt($curl, CURLOPT_HTTPHEADER,$headers); 29 30 if (!empty($arr['imgcode'])) { 31 curl_setopt($curl, CURLOPT_COOKIEFILE, $file); 32 } 33 34 $result = curl_exec ($curl); 35 curl_close ( $curl ); 36 echo $result;