A _php example of the authorization library for micro-credit Web pages based on CI framework

Source: Internet
Author: User
Tags foreach curl openid trim urlencode codeigniter zend framework

This paper illustrates the authorization Library of Micro-trust Web pages based on CI framework. Share to everyone for your reference, specific as follows:

This demonstrates the authorization function of the micro-credit page based on the CI framework.

1. Micro-trust Class library, Web page authorization 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 (' your appId ');
    $this->appsecret = Trim (' your appsecret '); function Redirect_url ($redirect) {/* Licensing page/$url = "Https://open.weixin.qq.com/connect/oauth2/author ize?appid= $this->appid&redirect_uri= $redirect &response_type=code&scope=snsapi_userinfo&
      State=state#wechat_redirect ";
    return $url; /* * to Exchange code for access_token*/function Access_token ($code) {/* acquired to 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 ']; /* Access to micro-credit user Data * * * $get _userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token= $access _token&openid=$
      OPENID&AMP;LANG=ZH_CN ";
      $userinfo = Json_decode (file_get_contents ($get _userinfo));
    return $userinfo; function eff ($access _token, $openid) {/* detect access_token correctly, errcode=0 as correct/$eff _url = "HTTPS://API.W
      eixin.qq.com/sns/auth?access_token= $access _token&openid= $openid ";
      $get _eff =json_decode (file_get_contents ($eff _url));
    return $get _eff; ///Curl to submit code in exchange for 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, 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; /* This position starts with the controller index () incoming micro-user data processing * * Function save_session ($data) {foreach ($data as $k
          EY => $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) {//convert array using Obj_to_arr method $data = Is_object ($data)? get_objeCt_vars ($data): $data;
        foreach ($data as $key => $value) {$arr [$key] = $value;
    return $arr;

 }
}

2. Get user information, controller file by code Exchange 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 ');
    /** * Coupon Main program/function index () {$this->load->view ('/coupon/index.html '); The function user_exists () {/* detects whether or not the micro-user exists $user _arr gets the micro-user information returned via Get_code, where the information is returned via a micro-trust server
        , you cannot record session $user Std_obj mode, convert an array $user _exists into model, check the data table for the existence of the user $redirect go through the process, jump to the home page
        If the function of the statement is to judge through the model to return the data table information, if for empty then the micro-letter user information into the table, and then read out, save into the session.
        Else then the data table already exists the user, read directly, save the session note that the reason for using foreach is a two-dimensional array of 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)) {/* The user data obtained by the micro-letter is Stdclass object format, so we need to convert the array to use the Obj_to_arr method
        *///Add custom characters into 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);
        /* by Coupon_idex get_coupon treatment/$this->session->set_userdata ($user _exists); if (Isset ($this-&GT;SESSION-&GT;return_url) Header (' Location: '. $this->session->return_url);
        Print_r ($user _exists); exit ();
    Header (' Location: '. $redirect);
      function Coupon_start () {/* Enter the pick page, need to be authorized first/$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);
        The Public Function Get_code () {if (Isset ($_get[' code ')) {$code = $_get[' code '];
        echo $code; exit ();
        $user _arr = $this->weixin->access_token ($code);
        Jump to User detection check_exists () go//Echo $user _arr;exit ();
        Var_dump ($user _arr);
      return $user _arr;
      }else{//Otherwise detect the presence of the user in the cookie, and if so, return to the page echo ' error '; }} public FuNction Coupon_get () {/* Get merchant bid, read the relevant 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);
      * * Thrown into the 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;

 }
}

More interested in CodeIgniter related content readers can view the site topics: "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "PHP Excellent Development Framework Summary", "thinkphp Introductory Course", " Thinkphp Common Methods Summary, "Zend Framework Introduction Course", "PHP object-oriented Programming Introduction Course", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on CodeIgniter framework.

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.