if (!isset($_GET ['code'])) {
// $selfUrl = 'http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['PHP_SELF'] . '?' . $_SERVER ['QUERY_STRING'];
$selfUrl = 'http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['PHP_SELF'];
$redirect_uri = urlencode($selfUrl);
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
header("location:" . $url);
} else {
$code = $_GET['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" .
$appSecret . "&code=" . $code . "&grant_type=authorization_code";
$weixin = file_get_contents($url); //通过code换取网页授权access_token
$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
$array = get_object_vars($jsondecode); //转换成数组
$openid = $array['openid']; //输出openid
$unionid = $array['unionid']; //输出unionid
if($openid){
$sql="INSERT INTO ".$ecs->table("weixin_user")." (ecuid,fake_id,unionid) VALUES ('$user_id','$openid','$unionid')";
$db->query($sql);
}
}