PHP 微信網頁授權開發


現在有大量的app分享 、點進去看視頻都微信授權登錄這一說,不過今天抽空把微信授權做了一下,也耗了點時間、授權回調頁面域名 這個地方剛開始做的時候也沒有顯示正確、redirect_url  參數錯誤,也就是你訪問你項目的地址、大家一定不要填錯了

第一步:百度搜索 微信公衆平臺 --> 登錄、進去完之後、往下拉看到微信開發者工具-》公衆平臺測試賬號。






///改你自己本地的域名

public function getOpendId(){
    $appid= "wx2e94e0f2c20f6144";
    $redirect_uri  = urlEncode('http://192.168.2.104/api.php/Register/getUserInfo'); 
    $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");
}
//獲取用戶信息
public function getUserInfo(){
    $code = $_GET['code'];
    $appid= "wx2e94e0f2c20f6144";
    $appsecret = "aec1349c37c1f8580525bc8c092fe491";
    //獲取access_token
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
    $WxCon = file_get_contents($url);
    $WxCon = json_decode($WxCon,true);
    $access_token = $WxCon['access_token'];
	//獲取用戶信息
    $userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$appid&lang=zh_CN";
    $userInfo = file_get_contents($userInfoUrl);
    echo '<pre>';
    $userArr = json_decode($userInfo,true);
    print_r($userArr);
}

  效果:


//然後對此進行一些處理 等- - 、

小夥伴們多看一下文檔:

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

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