public function getwx(){
$appid = 'wxxxxxxxxxxxxx';
$secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//獲取換openid的code
if (!isset($_GET['code'])) {
$baseUrl = 'http://www.xxx.com/home/index/index.html';//當前域名
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . urlencode($baseUrl) . '&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect';
header("Location:" . $url);
exit;
}
$code = strval($_GET['code']);
$state = strval($_GET['state']);
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code';
//$json_obj = getJson($get_token_url);
$json_obj=$this->getJson($get_token_url);
//根據(jù)openid和access_token查詢用戶信息
$access_token = $json_obj['access_token'];
$openid = $json_obj['openid'];
$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
獲取openid
$user_obj=$this->getJson($get_user_info_url);
//解析json
$_SESSION['user'] = $user_obj;
//包含文件
//實例化Mysqldb類
$user_id = $user_obj['openid']; //微信登陸號
$user_pic = addslashes($user_obj['user_pic']); //頭像
$user_name = addslashes($user_obj['nickname']);//名稱
$user_sex = addslashes($user_obj['sex']); //性別
$user_address = addslashes($user_obj['province']); //地址
//tp3 存入數(shù)據(jù)庫完成注冊登錄
$data['openid'] = $user_id;
$data['admin_name'] = $user_name;
$data['user_pic'] = $user_pic;
$data['sex'] = $user_sex;
$data['address'] = $user_address;
$user = M('user');
$userinfo = $user->where(array('openid' => $user_id))->find();
if(empty($userinfo)){
M('user')->add($data);
$userinfo = $user->where(array('openid' => $user_id))->find();
$_SESSION['home']=$userinfo;
$_SESSION['userid'] = $id;
$_SESSION['openid'] = $userinfo['openid'];
}else{
$_SESSION['home']=$userinfo;
$_SESSION['userid'] = $userinfo['userid'];
$_SESSION['openid'] = $userinfo['openid'];
}
}
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
php微信公眾號登錄注冊
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- title: 微信公眾號開發(fā):獲取openId和用戶信息 tags: 微信公眾號 categories: 筆記 ...
- 關(guān)于網(wǎng)頁授權(quán)回調(diào)域名的說明 1、在微信公眾號請求用戶網(wǎng)頁授權(quán)之前,開發(fā)者需要先到公眾平臺官網(wǎng)中的“開發(fā) - 接口權(quán)...