Example of WeChat webpage authorization library based on CI framework _ php instance

Source: Internet
Author: User
Tags openid
This article mainly introduces the web page authorization library based on the CI framework, and analyzes the integration authorization function of the CI framework and the implementation skills of controller calling in the form of examples, you can refer to the examples in this article to describe the web page authorization library based on the CI framework. We will share this with you for your reference. The details are as follows:

Here we will demonstrate the web page authorization function built on the CI framework.

1. small library, where webpage authorization is placed in the libraries folder

<? Phpif (! Defined ('basepath') exit ('no direct script access allowed'); Class Weixin {private $ appId; private $ appSecret; function _ construct () {$ this-> appId = trim ('your appid '); $ this-> appSecret = trim ('your appsecret');} function redirect_url ($ redirect) {/* authorization page */$ 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 ;} /* exchange code for access_token */function access_token ($ code) {/* get the code in exchange for access_token and 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 user data */$ 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) {/* check whether access_token is correct and errcode = 0 is correct */$ 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 ;} // use the curl method to submit the code in exchange for the access_token data 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, expires, FALSE); curl_seto Pt ($ 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;}/* controller index () incoming user data processing */function save_session ($ data) {foreach ($ data as $ ke Y => $ value) {// $ _ SESSION ['uid'] = $ value ['uid']; // $ _ SESSION ['nickname'] = $ value ['nickname']; // $ _ SESSION ['fullname'] = $ value ['fullname']; // $ _ SESSION ['status'] = $ value ['status']; // $ _ SESSION ['groupup'] = $ value ['groupup']; $ _ SESSION [$ key] = $ value;} return $ _ SESSION; // print_r ($ _ SESSION); exit (); // unset ($ _ SESSION [0]);} function obj_to_arr ($ data) {// convert to an array using the obj_to_arr method $ data = is _ Object ($ data )? Get_object_vars ($ data): $ data; foreach ($ data as $ key => $ value) {$ arr [$ key] = $ value;} return $ arr ;}}

2. use code for access_token to get user information, controller file

<? Phpif (! 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 ');} /*** main coupon program */function index () {$ this-> load-> view ('/coupon/index.html ');} Function User_exists () {/* checks whether the modified user exists $ user_arr obtains the user information returned through get_code. at this time, the information is returned through the server and the session $ user std_obj mode cannot be recorded, convert to an array $ user_exists and drop it into the model to check whether the user $ redirect exists in the data table has completed the process and then jump to the homepage if statement to determine whether the data table information is returned through the model, if it is null, enter the user information in the table, read it, and store it in the session. Else, the data table already exists in this user and can be read directly. Note that the reason for using foreach is because the data table is stored in the session, is a two-dimensional array to a one-dimensional array */$ 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 )) {/* because the obtained user data is in stdclass object format, you need to convert it to an array using the obj_to_arr method * // add custom characters to the array 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);/* processed by Get_Coupon in Coupon_idex */$ 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 () {/* to go to the receiving page, you must first authorize */$ 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); // jump to the user detection check_exists () go to // echo $ user_arr; exit (); // var_dump ($ user_arr); return $ user_arr;} else {// otherwise, check whether the user exists in the cookie. If yes, return to the home page echo 'error';} public function Coupon_Get () {/* get Merchant bid, read related information * // $ 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);/* throw it into Coupon_model, read the merchant information in 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 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.