Example of WeChat webpage authorization Library Based on CI framework and ci Framework Authorization example

Source: Internet
Author: User
Tags openid

Web page authorization library example based on CI framework, ci Framework Authorization example

This document describes 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 ;} /* Get access_token through code */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, 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, success, FALSE); curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALS E); 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;}/* 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: // response // $ 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/'.w.redirect_url); // $ redirect = urlencode ('HTTP: // response '); $ 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 ;}}

Related Article

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.