基于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;
}
原文地址:https://www.cnblogs.com/lmaster/p/6805284.html