博客列表 >6月1日作业

6月1日作业

老专的博客
老专的博客原创
2018年06月03日 16:55:191052浏览

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 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议