test3.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * 微信二维码登陆测试
  4. */
  5. include("../wechatauth.class.php");
  6. session_start();
  7. function logdebug($text){
  8. file_put_contents('../log/logwechat.txt',$text."\n",FILE_APPEND);
  9. };
  10. $sid = session_id();
  11. $options = array(
  12. 'account'=>$sid,
  13. 'datapath'=>'../log/cookiecode_',
  14. 'debug'=>true,
  15. 'logcallback'=>'logdebug'
  16. );
  17. $wechat = new Wechatauth($options);
  18. if (isset($_POST['code'])) {
  19. $logincode = $_POST['code'];
  20. $vres = $wechat->set_login_code($logincode)->verify_code();
  21. if ($vres===false) {
  22. $result = array('status'=>0);
  23. } else {
  24. $result = array('status'=>$vres);
  25. if ($vres==200) {
  26. $result['info'] = $wechat->get_login_info();
  27. $result['cookie'] = $wechat->get_login_cookie(true);
  28. }
  29. }
  30. die(json_encode($result));
  31. }
  32. $logincode = $wechat->get_login_code();
  33. $qrimg = $wechat->get_code_image();
  34. ?>
  35. <!DOCTYPE html>
  36. <html>
  37. <head>
  38. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  39. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  40. <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  41. <title>微信二维码登陆接口</title>
  42. </head>
  43. <body>
  44. <h2>微信里扫描以下二维码实现登陆</h2>
  45. <div id="content">
  46. <img src="<?php echo $qrimg;?>" />
  47. </div>
  48. <script type="text/javascript">
  49. var ajaxlock = false;
  50. var ajaxhandle;
  51. function synclogin(){
  52. if (!ajaxlock) {
  53. ajaxlock = true;
  54. $.post(location.href,{code:'<?php echo $logincode;?>'},function(json){
  55. //console.log(json);
  56. if (json.status) {
  57. console.log(json.status);
  58. if (json.status==200) {
  59. var nick,uid,username,sex,avatar;
  60. if (json.info && json.info.User){
  61. uid = json.info.User.Uin;
  62. nick = json.info.User.NickName;
  63. username = json.info.User.UserName;
  64. sex = json.info.User.Sex;
  65. avatar = json.info.User.HeadImgUrl;
  66. $('#content').html('<h2>用户信息</h2><ul><li><b>Uid:</b>'+uid
  67. +'</li><li><b>Nick:</b>'+nick
  68. +'</li><li><b>username:</b>'+username
  69. +'</li><li><b>sex:</b>'+(sex==1?'男':'女')
  70. +'</li><li><b>avatar:</b>'+avatar
  71. +'</li></ul>');
  72. }
  73. alert('login success, welcome '+nick);
  74. clearInterval(ajaxhandle);
  75. }
  76. }
  77. ajaxlock = false;
  78. },'json');
  79. }
  80. }
  81. $(function(){
  82. ajaxhandle = setInterval("synclogin()",5000);
  83. });
  84. </script>
  85. </body>