基于CI框架的微信網(wǎng)頁(yè)授權(quán)庫(kù)示例
本文實(shí)例講述了基于CI框架的微信網(wǎng)頁(yè)授權(quán)庫(kù)。分享給大家供大家參考,具體如下:
這里演示建立在CI框架上的微信網(wǎng)頁(yè)授權(quán)功能。
1. 微信小類(lèi)庫(kù),網(wǎng)頁(yè)授權(quán)放置在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)
{
/*授權(quán)頁(yè)面*/
$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;
}
/* 通過(guò)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'];
/*獲取微信用戶(hù)數(shù)據(jù)*/
$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)
{
/*檢測(cè)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;
}
//通過(guò)curl方式提交code換取access_token數(shù)據(jù)
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;
}
/*
這個(gè)位置開(kāi)始是控制器index()傳入的微信用戶(hù)資料處理
*/
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)
{
// 進(jìn)行轉(zhuǎn)換成數(shù)組 使用 obj_to_arr方式
$data = is_object($data)?get_object_vars($data):$data;
foreach ($data as $key => $value)
{
$arr[$key] = $value;
}
return $arr;
}
}
2. 通過(guò)code換access_token獲取用戶(hù)信息,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');
}
/**
*優(yōu)惠券主程序
*/
function index()
{
$this->load->view('/coupon/index.html');
}
function User_exists()
{
/*
檢測(cè)改微信用戶(hù)是否存在
$user_arr 獲取的是通過(guò)get_code返回的微信用戶(hù)信息,此時(shí)的信息是通過(guò)微信服務(wù)器返回的,不能記錄session
$user std_obj模式,轉(zhuǎn)換為數(shù)組
$user_exists 扔入model中,檢測(cè)數(shù)據(jù)表中是否存在該用戶(hù)
$redirect 走完流程后,跳轉(zhuǎn)到首頁(yè)
if語(yǔ)句的作用,是 判斷通過(guò)model返回?cái)?shù)據(jù)表的信息,如果為空則把微信用戶(hù)信息錄入到表中,再讀取出來(lái),存進(jìn)session。
else 則數(shù)據(jù)表已經(jīng)存在該用戶(hù),直接讀取,存進(jìn)session
需要注意的是,使用foreach的原因,是二維數(shù)組轉(zhuǎn)一維數(shù)組
*/
$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))
{
/*
由于微信獲取到的用戶(hù)數(shù)據(jù)是stdclass對(duì)象格式
所以需要進(jìn)行轉(zhuǎn)換成數(shù)組 使用 obj_to_arr方式
*/
//加入自定義的字符進(jìn)入數(shù)組
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()
{
/*進(jìn)入領(lǐng)取頁(yè)面,需要先經(jīng)過(guò)授權(quán)*/
$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);
//跳轉(zhuǎn)到用戶(hù)檢測(cè)中check_exists()去
// echo $user_arr;exit();
// var_dump($user_arr);
return $user_arr;
}else{
//否則檢測(cè)cookie中是否存在該用戶(hù),如果有,則return回首頁(yè)
echo 'error';
}
}
public function Coupon_Get()
{
/*獲取商家bid,讀取相關(guān)信息*/
// $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);
/*扔進(jìn)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;
}
}
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《ThinkPHP入門(mén)教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
- thinkphp整合微信支付代碼分享
- php版微信公眾平臺(tái)之微信網(wǎng)頁(yè)登陸授權(quán)示例
- 微信網(wǎng)頁(yè)授權(quán)(OAuth2.0) PHP 源碼簡(jiǎn)單實(shí)現(xiàn)
- PHP實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)開(kāi)發(fā)教程
- weiphp微信公眾平臺(tái)授權(quán)設(shè)置
- CI框架入門(mén)示例之?dāng)?shù)據(jù)庫(kù)取數(shù)據(jù)完整實(shí)現(xiàn)方法
- CI框架學(xué)習(xí)筆記(一) - 環(huán)境安裝、基本術(shù)語(yǔ)和框架流程
- CI框架中site_url()和base_url()的區(qū)別
- CI框架學(xué)習(xí)筆記(二) -入口文件index.php
相關(guān)文章
PHP中new static()與new self()的比較
在寫(xiě)代碼時(shí)發(fā)現(xiàn) new static(),覺(jué)得實(shí)例化的地方不是應(yīng)該是 new self()嗎?怎么回事?通過(guò)查閱相關(guān)資料才知道具體情況,下面小編整理下方便日后查找2016-08-08
PHP實(shí)現(xiàn)一個(gè)多功能購(gòu)物網(wǎng)站的案例
下面小編就為大家?guī)?lái)一篇PHP實(shí)現(xiàn)一個(gè)多功能購(gòu)物網(wǎng)站的案例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
php float不四舍五入截取浮點(diǎn)型字符串方法總結(jié)
在php中截取浮點(diǎn)型大致有以下幾種方法。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10
PHP后臺(tái)實(shí)現(xiàn)微信小程序登錄
這篇文章主要為大家詳細(xì)介紹了PHP后臺(tái)實(shí)現(xiàn)微信小程序登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
PHP中的常見(jiàn)魔術(shù)方法功能作用及用法實(shí)例
這篇文章主要介紹了PHP中的常見(jiàn)魔術(shù)方法功能作用及用法實(shí)例,本文講解了構(gòu)造函數(shù)和析構(gòu)函數(shù)__construct()和__desctruct()以及屬性重載(Property Overloading)__get()和、__set()、__isset()等等魔術(shù)方法,需要的朋友可以參考下2015-07-07
使用gd庫(kù)實(shí)現(xiàn)php服務(wù)端圖片裁剪和生成縮略圖功能分享
一般用戶(hù)上傳頭像時(shí),都會(huì)讓用戶(hù)自行裁剪圖片。那么php怎么實(shí)現(xiàn)這個(gè)功能呢?php中裁剪圖片主要使用gd庫(kù)的imagecopyresampled方法2013-12-12
VB中的RasEnumConnections函數(shù)返回632錯(cuò)誤解決方法
這篇文章主要介紹了VB中的RasEnumConnections函數(shù)返回632錯(cuò)誤解決方法,使用MSDN中的例子在XP SP3系統(tǒng)上出現(xiàn)的錯(cuò)誤,需要的朋友可以參考下2014-07-07
windows的文件系統(tǒng)機(jī)制引發(fā)的PHP路徑爆破問(wèn)題分析
這篇文章主要介紹了windows的文件系統(tǒng)機(jī)制引發(fā)的PHP路徑爆破問(wèn)題分析,需要的朋友可以參考下2014-07-07

