本文实例讲述了基于ci框架的微信网页授权库。分享给大家供大家参考,具体如下:
这里演示建立在ci框架上的微信网页授权功能。
1. 微信小类库,网页授权放置在libraries文件夹
<?php
if ( ! defined('basepath')) exit('no direct script access allowed');
class weixin
{
private $appid;
private $appsecret;
function __construct()
{
$this->appid = trim('你的appid');
$this->appsecret = trim('你的appsecret');
}
function redirect_url($redirect)
{
/*授权页面*/
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appid&redirect_uri=$redirect&response_type=code&scope=snsapi_userinfo&state=state#wechat_redirect";
return $url;
}
/* 通过code换取access_token*/
function access_token($code)
{
/*获取到的code换取access_token和openid*/
$post_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->appsecret&code=$code&grant_type=authorization_code";
// echo $post_url;exit();
$return = $this->postdata($post_url);
// print_r($return);exit();
$access_token = $return['access_token'];
$openid = $return['openid'];
/*获取微信用户数据*/
$get_userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_cn";
$userinfo = json_decode(file_get_contents($get_userinfo));
return $userinfo;
}
function eff($access_token,$openid)
{
/*检测access_token是否正确,errcode=0 为正确*/
$eff_url = "https://api.weixin.qq.com/sns/auth?access_token=$access_token&openid=$openid";
$get_eff =json_decode(file_get_contents($eff_url));
return $get_eff;
}
//通过curl方式提交code换取access_token数据
function postdata($url)
{
header('content-type:text/html;charset=utf-8');
// echo $url;exit();
$curl = curl_init();
curl_setopt($curl, curlopt_url, $url);
curl_setopt($curl, curlopt_ssl_verifypeer, false);
curl_setopt($curl, curlopt_ssl_verifyhost, false);
curl_setopt($curl, curlopt_sslversion, 1);
// if (!empty($data)){
// curl_setopt($curl, curlopt_post, 1);
// curl_setopt($curl, curlopt_postfields, $data);
// }
curl_setopt($curl, curlopt_returntransfer, 1);
$output = curl_exec($curl);
curl_close($curl);
// var_dump($output);exit();
// print_r($output);exit();
$access = json_decode($output,true);
return $access;
}
/*
这个位置开始是控制器index()传入的微信用户资料处理
*/
function save_session($data)
{
foreach ($data as $key => $value) {
// $_session['uid'] = $value['uid'];
// $_session['nickname'] = $value['nickname'];
// $_session['fullname'] = $value['fullname'];
// $_session['status'] = $value['status'];
// $_session['groups'] = $value['groups'];
$_session[$key] = $value;
}
return $_session;
// print_r($_session);exit();
// unset($_session[0]);
}
function obj_to_arr($data)
{
// 进行转换成数组 使用 obj_to_arr方式
$data = is_object($data)?get_object_vars($data):$data;
foreach ($data as $key => $value)
{
$arr[$key] = $value;
}
return $arr;
}
}
2. 通过code换access_token获取用户信息,controller文件
<?php
if ( ! defined('basepath')) exit('no direct script access allowed');
class coupon_index extends ci_controller
{
function __construct()
{
parent::__construct();
$this->load->library(array('weixin','session'));
$this->load->helper('url');
// $this->load->ldap_mod_del(link_identifier, dn, entry)
$this->load->model('coupon_model');
}
/**
*优惠券主程序
*/
function index()
{
$this->load->view('/coupon/index.html');
}
function user_exists()
{
/*
检测改微信用户是否存在
$user_arr 获取的是通过get_code返回的微信用户信息,此时的信息是通过微信服务器返回的,不能记录session
$user std_obj模式,转换为数组
$user_exists 扔入model中,检测数据表中是否存在该用户
$redirect 走完流程后,跳转到首页
if语句的作用,是 判断通过model返回数据表的信息,如果为空则把微信用户信息录入到表中,再读取出来,存进session。
else 则数据表已经存在该用户,直接读取,存进session
需要注意的是,使用foreach的原因,是二维数组转一维数组
*/
$user_arr = $this->get_code();
// var_dump($user_arr);exit();
$user = $this->weixin->obj_to_arr($user_arr);
// var_dump($user);exit();
// print_r($user);exit();
$user_exists = $this->coupon_model->checkuser('cou_user',$user);
// print_r($user_exists);exit();
// $redirect = 'http://yourwebname.com/coupon/index.php/coupon/coupon_index/coupon_get/bid/1';
// $return_url = $this->session->return_url;
$redirect = 'http://yourwebname.com'.$this->session->return_url;
// echo $redirect;exit();
if(empty($user_exists))
{
/*
由于微信获取到的用户数据是stdclass对象格式
所以需要进行转换成数组 使用 obj_to_arr方式
*/
//加入自定义的字符进入数组
unset($user['privilege']);
$user_exists['nickname'] = $user['nickname'];
$user_exists['openid'] = $user['openid'];
$user_exists['language'] = $user['language'];
$user_exists['city'] = $user['city'];
$user_exists['country'] = $user['country'];
$user_exists['province'] = $user['province'];
$user_exists['headimgurl'] = $user['headimgurl'];
$user_exists['sex'] = $user['sex'];
$user_exists['fullname'] = $user['nickname'];
$user_exists['telphone'] = '';
$user_exists['login_ip'] =$this->input->ip_address();
$user_exists['last_ip'] =$this->input->ip_address();
$user_exists['groups'] = register_group_id;
$user_exists['status'] = 1;
$user_exists['login_time'] = date("y-m-d");
$insert_id = $this->coupon_model->insert_one('cou_user',$user_exists);
$user_exists['uid'] = $insert_id;
}
else{
$user_exists = $user_exists[0];
}
// $return_url = $this->session->back_url;
// if(isset($return_url))header('location:'.$return_url);
/*由coupon_idex中的get_coupon处理*/
$this->session->set_userdata($user_exists);
if(isset($this->session->return_url))header('location:'.$this->session->return_url);
// print_r($user_exists);exit();
header('location:'.$redirect);
}
function coupon_start()
{
/*进入领取页面,需要先经过授权*/
$redirect_url = 'coupon/coupon_index/user_exists';
$redirect = urlencode('http://yourwebname.com/coupon/index.php/'.$redirect_url);
// $redirect = urlencode('http://yourwebname.com/coupon/index.php/coupon/coupon_index/get_code');
$return = $this->weixin->redirect_url($redirect);
header('location:'.$return);
}
public function get_code()
{
if(isset($_get['code']))
{
$code = $_get['code'];
// echo $code;exit();
$user_arr = $this->weixin->access_token($code);
//跳转到用户检测中check_exists()去
// echo $user_arr;exit();
// var_dump($user_arr);
return $user_arr;
}else{
//否则检测cookie中是否存在该用户,如果有,则return回首页
echo 'error';
}
}
public function coupon_get()
{
/*获取商家bid,读取相关信息*/
// $b_name = $this->uri->segment(4, 0);
$nickname = $this->session->nickname;
$openid = $this->session->openid;
$status = $this->session->status;
$_session['return_url'] = $_server['request_uri'];
// $this->session->set_userdata($return_url);
// echo $this->session->return_url;exit();
if(empty($nickname))header('location:'.'http://yourwebname.com/coupon/index.php/coupon/coupon_index/coupon_start');
$bid = $this->uri->segment(5, 0);
/*扔进coupon_model中,读取bid中的商家信息*/
$content = $this->coupon_model->coupon_business('cou_business',$bid);
// print_r($content);
// echo $bid;
// echo $b_name;
$data['bname'] = $content['bname'];
$data['discount'] = $content['discount'];
$data['bimg'] = $content['bimg'];
$data['contents'] = $content['contents'];
$data['amount'] = $content['amount'];
$data['nickname'] = $nickname;
$data['status'] = $status;
$data['js'] = json_encode(array($content['bname'],$content['discount'],$nickname,$status));
// echo $data['js'];exit();
// print_r($data);
$this->load->view('/coupon/index.html',$data);
// echo $nickname;
// echo $status;
}
}
更多关于codeigniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php优秀开发框架总结》、《thinkphp入门教程》、《thinkphp常用方法总结》、《zend framework框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于codeigniter框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!