zoukankan      html  css  js  c++  java
  • 基于tinkphp3.2获取openid

    <?php
    namespace HomeController;
    
    use ThinkController;
    
    /**
     * 基础
     */
    class BaseController extends Controller
    {
    
    
        private $appid  = "wx9b163a7cb6c74fdd";
        private $secret = "f7de758264ff66a8178b8e6fa2379ed5";
    
        
        protected function doRedirect()
        {
            $redirect_uri = urlencode(getUrlInfor());
    
            $scope = 'snsapi_userinfo';
            //$scope = 'snsapi_base';
            $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->appid;
            $url .= '&redirect_uri=' . $redirect_uri . '&response_type=code';
            $url .= '&scope=' . $scope . '&state=123#wechat_redirect';
            header('location:' . $url);
        }
    
        /**
         * 根据openid和access_token查询用户信息
         * @author 梁景
         * @date   2017-04-27
         * @param  [type]     $data [description]
         * @return [type]           [description]
         */
        protected function getUserInfor($data)
        {
    
            $access_token = $data['access_token'];
            $openid       = $data['openid'];
    
            $get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
    
            $res = httpGet($get_user_info_url);
    
            $user_obj = json_decode($res, true);
            return $user_obj;
        }
    
        /**
         * 获取token
         * @author 梁景
         * @date   2017-04-27
         * @param  [type]     $code [description]
         * @return [type]           [description]
         */
        protected function getTokenInfor($code)
        {
    
            $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . $code . '&grant_type=authorization_code';
    
            return json_decode(httpGet($get_token_url), true);
        }
    }

    httpGet放入通用函数中

    /**
     * get获取
     * @author 梁景
     * @date   2017-04-27
     * @param  [type]     $url [description]
     * @return 返回页面内容
     */
    function httpGet($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_URL, $url);
    
        $res = curl_exec($curl);
        curl_close($curl);
    
        return $res;
    }
  • 相关阅读:
    小程序开发-7-访问api数据与ES6在小程序中的应用
    小程序开发-8-流行页面编码与组件的细节知识
    小程序开发-6-组件数据、事件与属性
    当安装mongodb客户端出现了Failed to load list of databases
    对bluebird的理解
    百度地图实现案例
    iScroll实现下拉刷新上拉加载
    nodejs环境变量配置
    检测Python程序本身是否已经在运行
    用Python快速找到出现次数最多的数据
  • 原文地址:https://www.cnblogs.com/lmaster/p/6805284.html
Copyright © 2011-2022 走看看