博主信息
博文 55
粉丝 0
评论 0
访问量 38565
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
6月1日作业
老专的博客
原创
1211人浏览过

6月1日作业

* 微信网页授权

 * 1、回调域名;按微信规范

 * 2、以snsapi_base为scope 和 以snsapi_userinfo为scop 发起的网页授权

 * 3、网页授权access_token和普通access_token

 * 4、同一用户的 UnionID 相同

 * 5、网页授权流程分四步:

 *    1)、引导用户进入授权页面同意授权,获取code

 *    2)、通过code换取网页授权access_token(与基础支持中的access_token不同)

 *    3)、如果需要,开发者可以刷新网页授权access_token,避免过期

 *    4)、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)

url: www.php.wx/index.php/index/weixin_index/auth

实例

<?php
namespace app\index\controller;

use think\Controller;
use think\facade\Cache;

class Weixin_index extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->model = model('Weixin'); //开发使用,其他方法调用模型时方便修改
    }
    
    // 1、微信网页授权
    // 获取当前用户的昵称、头像信息
        // 1)、 第一步:用户同意授权,获取code
	// 2)、 第二步:通过code换取网页授权access_token
	// 3)、 第三步:刷新access_token(如果需要)
	// 4 )、第四步:拉取用户信息(需scope为 snsapi_userinfo)
    public function auth(){
        //第一步:用户同意授权,获取code
        $redirect = 'http://www.c-air-c.com/index.php/weixintest/userinfo';
        $url_code = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('app.appid').'&redirect_uri='.urlEncode($redirect).'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        header('Location:'.$url_code);
    }
    
    //显示用户信息
    public function userinfo(){
        // 获取code
        $code = input('get.code');

        // 第二步:通过code换取网页授权access_token
        $res = $this->model->auth_access_token($code,false);
        $auth_access_token = $res['access_token'];
        $openid = $res['openid'];
        // 第三步:拉取用户信息(需scope为 snsapi_userinfo)
        $userinfo = $this->model->get_userinfo($auth_access_token,$openid);
        dump($userinfo);
    }
    
    public function demo1()
    {
        echo '微信测试';
    }

}

运行实例 »

点击 "运行实例" 按钮查看在线实例

url: www.php.wx/index.php/index/weixin_model

实例

?php
namespace app\index\model;
use think\Model;
use think\facade\Cache;
use think\Db;

class Weixin extends Model
{
     //普通授权 access_tocken(虽然名称与网页授权相同,但内容不同)
    public function access_token($iscache = true){
            $key = 'access_token';
    if(!$iscache){
        Cache::rm($key);
    }
            $data = Cache::get($key);
            if($data && $iscache){
                    return $data;
            }
            $appid = config('app.appid');
    $appsecret = config('app.appsecret');
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
    $res = http_get($url);
    $res = json_decode($res,true);
    if(!isset($res['access_token'])){
            return false;
    }
    Cache::set($key,$res['access_token'],($res['expires_in']-100));
    return $res['access_token'];
    }

    //网页授权 access_token(虽然名称与普通授权相同,但内容不同)
    public function auth_access_token($code)
    {
        $appid = config('app.appid');
        $appsecret = config('app.appsecret');
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        $res = http_get($url);
        $res = json_decode($res,true);
        if(!isset($res['access_token'])){
            return false;
        }
        return $res;
    }
    
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

url: www.php.wx/index.php/index/common

实例

<?php
// 应用公共文件
function http_Post($url,$data){
	$curl = curl_init();
	curl_setopt($curl,CURLOPT_URL,trim($url));
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	//启用时会发送一个常规的POST请求,为1或者为true
	if(!empty($data)){
		$data = is_array($data)?json_encode($data):$data;
		curl_setopt($curl,CURLOPT_POST,1);
		curl_setopt($curl,CURLOPT_POSTFIELDS,$data);//需要要传送的内容
	}
	curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
	$return_str = curl_exec($curl);
	curl_close($curl);
	return $return_str;
}

function http_Get($url){
	$curl = curl_init();
	curl_setopt($curl,CURLOPT_URL,trim($url));
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl,CURLOPT_HEADER,0);
	curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'GET');//需要要传送的内容
	curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
	$return_str = curl_exec($curl);
	curl_close($curl);
	return $return_str;
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学