APP微信快捷登錄

1.準備工作

在開發這個接口的時候,事先需要獲取appid和secret這兩個參數然後纔可以進行下一步的開發,http://zhidao.baidu.com/link?url=*********************

2.正式開發

我先上代碼再說

 public function weixinlogin()
    {
        $code=self::get('code');
        $content = file_get_contents('https://api.weixin.qq.com/sns/oauth2/access_token?appid=*******=' . $code . '&grant_type=authorization_code');
        $weixintoken = json_decode($content, true);
        if (! empty($weixintoken['unionid'])) {
            $user = file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token=' . $weixintoken['access_token'] . '&openid=' . $weixintoken['openid']);
            $weixinuserinfo = json_decode($user, true);
        }
    }

在這裏客戶端會給接口傳一個code值,這個是客戶端可以獲得的,訪問
 $content = file_get_contents('https://api.weixin.qq.com/sns/oauth2/access_token?appid=*******&secret=**********&code=' . $code . '&grant_type=authorization_code')
會獲取到返回值,,然後去訪問
$user = file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token=' . $weixintoken['access_token'] . '&openid=' . $weixintoken['openid']);
會返回用戶的unionid值,在項目中可以把這個值跟用戶信息一起存到數據庫做記錄,在這個返回值中還會返回用戶最新用戶名跟頭像更新到數據庫裏就可以了(根據自己項目流程決定)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章