-
- 1 先用户授权,获取code
- 2 根据获取的code获取access{-token
- 3 通过access—token 和openID获取用户信息
- <?php
- namespace Home\Controller;
- use Think\Controller;
- class IndexController extends Controller {
- public function index(){
- //公众号
- $appid='wx922334ac8ba7e724';
- $redirect_uri=urlencode('http://www.foreverhao.top/vote/index.php/home/index/getcode');//将字符串以url编码
- $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);//header函数向客户发送原始的HTTP报头
- }
- public function getcode(){
- $code=$_GET['code'];//预定义$_GET变量用于收集来自method="get"的表单中的值
- echo $code;
- // $json = $this->access_token($code);
- // echo $json;
- }
public function getcode(){ $code=$_GET['code'];//预定义$_GET变量用于收集来自method="get"的表单中的值 // echo $code; $json = $this->access_token($code); echo $json; } //获取access_token private function access_token($code){ $appid="wx922334ac8ba7e724 "; $appsecret="ed5e2c9a0983442d59751cb0bb863870"; $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code"; $ret=https_request($url); return $ret; }
实现微信网页授权
最新推荐文章于 2022-10-25 08:00:11 发布