


The example in this article describes the WeChat webpage authorization library based on the CI framework. Share it with everyone for your reference, the details are as follows:
Here is a demonstration of the WeChat web page authorization function built on the CI framework.
1. WeChat small class library, web page authorization is placed in the libraries folder
<?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. Obtain user information and controller file by exchanging code for access_token
<?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; } }
Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "ThinkPHP Summary of common methods", "Zend FrameWork framework introductory tutorial", "php object-oriented programming introductory tutorial", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.

微信文件的过期时间需要根据情况来判断:1、如果发送的文件没有打开过,则在72小时以后微信系统会自动清理掉,即过了三天文件就会过期;2、如果已经查看了微信文件,但是并没有下载(当然已经下载的文件也是一样的),那么文件是可以保留180天的,在这180天以内随时都可以去下载。

区别:1、拉黑后对话框从主页消失,但是聊天记录还在;删除后聊天记录全部消失不见了。2、拉黑后还能发给他,但是收不到他的消息;删除后不能发信息了。3、拉黑后双方都不可见彼此的朋友圈;删除对方以后,你看不到对方的朋友圈了,对方是否能看到你的,取决于设置(允许陌生人查看十张照片)与否,如果设置则可以看到朋友圈。

支持微信付款的购物平台有:1、京东,是中国的综合网络零售商;2、唯品会,是一家在线销售品牌折扣商品的互联网公司;3、拼多多,是社交新电商领导者,更懂消费者的购物平台;4、京喜,是京东旗下生活消费商城;5、蘑菇街,一个电子商务网站;6、聚美优品,是一家以销售化妆品为主的时尚购物网站;7、微店,是一个云推广电子商务平台;8、考拉海购,是一个跨境海淘业务为主的会员电商平台。

可以。未经过实名认证的微信号,可以绑定他人的银行卡,但在绑定过程中需要提供银行卡的开户人姓名、开户行地址、开户时预留的联系方式及银行卡支付密码;已通过实名认证的微信号,无法绑定他人银行卡,只能添加使用自己身份证办理的银行卡。

财付通是微信,是腾讯公司旗下的第三方支付平台,其核心业务是协助在互联网上进行交易的双方完成支付和收款,其使用方式是:1、进行账户注册及登录;2、进行账户充值;3、根据需求设置快捷支付;4、通过打开微信支付或QQ钱包查询交易账单。

电脑微信打字打一个少一个是因为开启了改写状态,其解决办法:1、打开电脑微信;2、在微信聊天窗口输入对话文字内容;3、找到并按下键盘上的Insert键即可正常输入文字内容。

不是,一个身份证能绑定5个微信。按照微信当前规定,一个身份证可以实名认证5个微信号;如果已经实名认证了5个微信账号,但是还想要继续实名,就要把已经实名认证的一些不用的微信号清除以后,才可以再实名认证新的微信号。

区别:1、赞赏码是用于别人给自己打赏的,收取小费等小金额的赞赏给予,而收款码是一般的收款行为,可以进行大额收费的二维码;2、收款码是随时会变的,如果不是商家收款码,每次打开都会变,但是赞赏码不同,赞赏码是不会变的;3、赞赏码只能进行小额的首款,而收款码将可以大额首款。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools