代码很少
//获取code
$appid='xxxxxx';
$redirect_uri = urlencode ('http://xxxxxxxxxxxx');
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
header("Location:".$url);
if (!empty($_GET['code']))
{
$appid = C('APPID');
$secret = C('SECRET');
$code = $_GET['code']; //通过code换取token
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
$json = file_get_contents($url);
$arr = json_decode($json,true);
$token = $arr['access_token'];
$openid = $arr['openid'];
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid&lang=zh_CN"; //拿到token后获取用户基本信息
$json = file_get_contents($url); //获取微信用户基本信息
$arr = json_decode($json,true);
}