明凯博客

关注网站技术,一个特立独行的程序员

CodeIgniter中配置微信网页Oauth2.0类库

一、首先在config文件夹下新建文件配置文件wx_oauth.php

< ?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 用于验证微信接口配置信息的Token,可以任意填写
$config['token'] = '接口配置信息的Token';
 
// appID
$config['appid'] = '你的appid';
 
// appSecret
$config['secret'] = '你的secret';
 
// 回调链接地址
$config['redirect_uri'] = '你需要设置的回掉地址';
 
// 是否以 HTTPS 安全协议访问接口
$config['https_request']        = true;
 
// 授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
// snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,
// 即使在未关注的情况下,只要用户授权,也能获取其信息)
$config['scope'] = 'snsapi_userinfo';
 
// 语言
$config['lang'] = 'zh_CN'; // zh_CN 简体,zh_TW 繁体,en 英语
 
// 微信公众账户授权地址
$config['mp_authorize_url'] = 'https://api.weixin.qq.com/cgi-bin/token';
// 授权地址
$config['authorize_url'] = 'https://open.weixin.qq.com/connect/oauth2/authorize';
// 获取access token 的地址
$config['access_token_url'] = 'https://api.weixin.qq.com/sns/oauth2/access_token';
// 刷新 token 的地址
$config['refresh_token_url'] = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';
// 获取用户信息地址
$config['userinfo_url'] = 'https://api.weixin.qq.com/sns/userinfo';
// 验证access token
$config['valid_token_url'] = 'https://api.weixin.qq.com/sns/auth';

二、编写ci类库文件wx_oauth.php

< ?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
/**
* Weixin_oauth 类库,主要是远程抓取页面,默认http请求,也可以使用https请求,
* 可以在初始化的时候通过传入可选参数https为true,设置为https请求
*/
class Wx_oauth
{
        const TIMEOUT = 5;                        // 设置超时时间
 
        private $ci;                                // CI对象
        private $ch;                                // curl对象
        
        function __construct()
        {
                $this->ci =& get_instance();
                $this->ci->config->load('wx_oauth');                // 载入配置文件
        }
 
        /**
         * 验证配置接口信息
         * @param array 从微信接口发送来的信息,通过$_GET获得
         */
        public function interface_valid($get_request)
        {
                $signature = $get_request['signature'];
                $timestamp = $get_request['timestamp'];
                $nonce = $get_request['nonce'];        
 
                $token = $this->ci->config->item('token');
                $tmpArr = array($token, $timestamp, $nonce);
                sort($tmpArr);
                $tmpStr = implode( $tmpArr );
                $tmpStr = sha1( $tmpStr );
 
                if( $tmpStr == $signature ){
                        echo $get_request['echostr'];
                        exit;
                }
        }
 
        /**
         * 生成用户授权的地址
         * @param string 自定义需要保持的信息
         * @param bool 是否是通过公众平台方式认真
         */
        public function authorize_addr($state='', $mp=false)
        {
                if ($mp) {
                        $data = array(
                                'appid'=>$this->ci->config->item('appid'),
                                'secret'=>$this->ci->config->item('secret'),
                                'grant_type'=>'client_credential');
                        $url = $this->ci->config->item('mp_authorize_url');
                } else {
                        $data = array(
                                'appid'=>$this->ci->config->item('appid'),
                                'redirect_uri'=>urlencode($this->ci->config->item('redirect_uri')),
                                'response_type'=>'code',
                                'scope'=>$this->ci->config->item('scope'),
                                'state'=>$state,
                                '#wechat_redirect'=>'');
                        $url = $this->ci->config->item('authorize_url');
                }
                
                return $url . $this->create_url($data);
        }
 
        /**
         * 获取 access token
         * @param string 用于换取access token的code,微信提供
         */
        public function access_token($code)
        {
                $data = array(
                        'appid'=>$this->ci->config->item('appid'),
                        'secret'=>$this->ci->config->item('secret'),
                        'code'=>$code,
                        'grant_type'=>'authorization_code');
                // 生成授权url
                $url = $this->ci->config->item('access_token_url');
                return $this->send_request($url, $data);
        }
 
        /**
         * 刷新 access token
         * @param string 用于刷新的token
         */
        public function refresh_token($refresh_token)
        {
                $data = array(
                        'appid'=>$this->ci->config->item('appid'),
                        'refresh_token'=>$refresh_token,
                        'grant_type'=>'refresh_token');
                // 生成授权url
                $url = $this->ci->config->item('refresh_token_url');
                return $this->send_request($url, $data);
        }
 
        /**
         * 获取用户信息
         * @param string access token
         * @param string 用户的open id
         */
        public function userinfo($token, $openid)
        {
                $data = array(
                        'access_token'=>$token,
                        'openid'=>$openid,
                        'lang'=>$this->ci->config->item('lang'));
                // 生成授权url
                $url = $this->ci->config->item('userinfo_url');
                return $this->send_request($url, $data);
        }
 
        /**
         * 检验access token 是否有效
         * @param string access token
         * @param string 用户的open id
         */
        public function valid($token, $openid)
        {
                $data = array(
                        'access_token'=>$token,
                        'openid'=>$openid);
                // 生成授权url
                $url = $this->ci->config->item('valid_token_url');
                return $this->send_request($url, $data);
        }
 
        /**
         * 发送curl请求,并获取请求结果
         * @param string 请求地址
         * @param array 如果是post请求则需要传入请求参数
         * @param string 请求方法,get 或者 post, 默认为get
         * @param bool 是否以https协议请求
         */
        private function send_request($request, $params, $method='get', $https=true)
        {
                // 以get方式提交
                if ($method == 'get') {
                        $request = $request . $this->create_url($params);
                }
 
                $this->ch = curl_init($request);
                curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);// 设置不显示结果,储存入变量
                curl_setopt($this->ch, CURLOPT_TIMEOUT, self::TIMEOUT); // 设置超时限制防止死循环
 
                // 判断是否以https方式访问
                if ($https) {
                        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
                }
 
                if ($method == 'post') {        // 以post方式提交
                        curl_setopt($this->ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $params); // Post提交的数据包
                }
                
                $tmpInfo = curl_exec($this->ch); // 执行操作
                if (curl_errno($this->ch)) {
                        echo 'Errno:'.curl_error($this->ch);//捕抓异常
                }
                curl_close($this->ch); // 关闭CURL会话
 
                return $tmpInfo; // 返回数据
        }
 
        /**
         * 生成url
         */
        private function create_url($data)
        {
                $temp = '?';
                foreach ($data as $key => $item) {
                        $temp = $temp . $key . '=' . $item . '&';
                }
                return substr($temp, 0, -1);
        }
}

3、控制器中调用类文件

< ?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Site extends CI_Controller {
        public function __construct()
        {
                parent::__construct();
                $this->load->helper('url');
                $this->load->library('wx_oauth');
        }
 
        public function index()
        {
                $addr = $this->wx_oauth->authorize_addr();
                echo anchor($addr, 'authorize!!!');
        }
 
        public function callback()
        {
                $code = $_GET['code'];
                $response = $this->wx_oauth->access_token($code);
                print_r($r = json_decode($response));
                echo anchor('site/refresh/'.$r->refresh_token, 'refresh'),'
'; echo anchor('site/info/'.$r->access_token.'/'.$r->openid, 'user info'),'
'; echo anchor('site/valid/'.$r->access_token.'/'.$r->openid, 'valid access'); } public function refresh($refresh_token) { $response = $this->wx_oauth->refresh_token($refresh_token); print_r(json_decode($response)); } public function info($access_token, $openid) { header("Content-type: text/html; charset=gbk"); $response = $this->wx_oauth->userinfo($access_token, $openid); print_r(json_decode($response)); } public function valid($access_token, $openid) { $response = $this->wx_oauth->valid($access_token, $openid); print_r(json_decode($response)); } }

4、测试微信接口,用微信访问网页

, ,

相关文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注