1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > php 微信公众号登录 PHP 实现微信公众号网页授权登录

php 微信公众号登录 PHP 实现微信公众号网页授权登录

时间:2018-09-01 17:32:51

相关推荐

php 微信公众号登录 PHP 实现微信公众号网页授权登录

‍‍

第一步:index.php页面,用户同意授权,获取code<?php

$appid='APPID';

$redirect_uri=urlencode('/test.php');//重定向地址

$url="https://open./connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

header("Location:".$url);

第二步:test.php页面,通过返回的code获取网页授权的access_token<?php

$appid="APPID";

$secret="APPSECRET";

$code=$_GET["code"];

$oauth2Url="https://api./sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";

$oauth2=getJson($oauth2Url);

//获得access_token和openid

$access_token=$oauth2["access_token"];

$openid=$oauth2['openid'];

functiongetJson($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);

returnjson_decode($output,true);

}

第三步:通过access_token和openid获取用户的信息$get_user_info_url="https://api./sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";

$userinfo=getJson($get_user_info_url);

//打印用户信息

print_r($userinfo);

//array('openid'=>'oiuH-xxxxxxxxxxxxx',

'sex'=>1,

'language'=>'zh_CN',

'city'=>'南宁',

'province'=>'广西',

'country'=>'中国',

'headimgurl'=>'',

'privilege'=>array()

);

‍‍

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。