亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

最近開發(fā)微信,涉及到微信用戶登錄的問題,研究了一下還是搞定了,整理了一下跟大家貼出來我寫的這個代碼吧,需要用的東西我都已經(jīng)定義成常量,需要的話直接修改,以下是代碼:

<?php
namespace Apps\Controller;
use Common\Controller\AppBaseController;
define("TOKEN", "");//你微信定義的token
define("APPID", "");//你微信定義的appid
define("APPSECRET","");//你微信公眾號的appsecret
class TestController extends AppBaseController //繼承的這個控制器沒用 你可以繼承其他的任意一個
{
    //第一步:用戶同意授權,獲取code
    function accept(){
        //這個鏈接是獲取code的鏈接 鏈接會帶上code參數(shù)
        $REDIRECT_URI = "http://www.yourdomain.com/wxt_webhome/index.php/Apps/Test/getCode";
        echo $REDIRECT_URI."<br>";
        $REDIRECT_URI = urlencode($REDIRECT_URI);
        echo $REDIRECT_URI."<br>";
        $scope = "snsapi_userinfo";
        echo $scope."<br>";
        $state = md5(mktime());
        echo $state."<br>";
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".$REDIRECT_URI."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
        header("location:$url");
        
    }
    //用戶同意之后就獲取code  通過獲取code可以獲取一切東西了  機智如我
    function getCode(){
        //獲取accse_token
        $code = I("code");
        //echo $code;
        //echo "<br>";
        //用code獲取access_yoken
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$code."&grant_type=authorization_code";
        //這里可以獲取全部的東西  access_token openid scope
        $res = $this->https_request($url);
        $res  = json_decode($res,true);
        $openid = $res["openid"];
        echo "<pre>";
        //print_r($res);
        //echo $openid;
        //echo "<br>";
        $access_token = $res["access_token"];
        //echo $access_token;
        //這里是獲取用戶信息
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $res = $this->https_request($url);
        $res = json_decode($res,true);
        //寫入session
        print_r($res);
        //把用戶的信息寫入session 以備查用
        $weixn = $res["openid"];
        $nickname = $res["nickname"];
        session('weixin',$weixin);
        header("location:http://www.yourdomain.com/wxt_webhome/test.php");
    }
    function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}
}//classend


分享到:
標簽:thinkphp3.2.3 微信授權登錄 微信教程
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定