开始登陆 login.php

//-------配置
$AppID = 'wx033336c794d4';
$AppSecret = 'd4624c36333337af5443d';
$callback = 'http://www.php127.php/callback.php'; //回调地址

//微信登录 login.php
session_start();
//-------生成唯一随机串防CSRF攻击
$state = md5(uniqid(rand(), TRUE));
$_SESSION["wx_state"] = $state; //存到SESSION
$callback = urlencode($this->callback);
$wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$AppID."&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
header("Location: $wxurl");

回调地址 callback.php

  1. //验证CSRF攻击
  2. if($_GET['state']!=$_SESSION["wx_state"]){
  3.       exit("5001");
  4. }
  5. $AppID = 'wx33333333334d4';
  6. $AppSecret = 'd4624c363333330547af5443d';
  7. $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';
  8. $ch = curl_init();
  9. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  11. curl_setopt($ch, CURLOPT_URL, $url);
  12. $json =  curl_exec($ch);
  13. curl_close($ch);
  14. $arr=json_decode($json,1);
  15. //得到 access_token 与 openid
  16. print_r($arr);    
  17. $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN';
  18. $ch = curl_init();
  19. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  21. curl_setopt($ch, CURLOPT_URL, $url);
  22. $json =  curl_exec($ch);
  23. curl_close($ch);
  24. $arr=json_decode($json,1);
  25. 得到 用户资料
  26. print_r($arr);    

最终输出如图所示

thinkphp微信PC扫码授权登陆-PHPACG博客