最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于CI框架的微信網(wǎng)頁(yè)授權(quán)庫(kù)示例

 更新時(shí)間:2016年11月25日 08:47:16   作者:林偉盛  
這篇文章主要介紹了基于CI框架的微信網(wǎng)頁(yè)授權(quán)庫(kù),結(jié)合實(shí)例形式分析了CI框架整合微信授權(quán)功能及控制器調(diào)用的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了基于CI框架的微信網(wǎng)頁(yè)授權(quán)庫(kù)。分享給大家供大家參考,具體如下:

這里演示建立在CI框架上的微信網(wǎng)頁(yè)授權(quán)功能。

1. 微信小類庫(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'];
      /*獲取微信用戶數(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è)位置開始是控制器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)
    {
      // 進(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獲取用戶信息,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è)改微信用戶是否存在
        $user_arr 獲取的是通過(guò)get_code返回的微信用戶信息,此時(shí)的信息是通過(guò)微信服務(wù)器返回的,不能記錄session
        $user std_obj模式,轉(zhuǎn)換為數(shù)組
        $user_exists 扔入model中,檢測(cè)數(shù)據(jù)表中是否存在該用戶
        $redirect 走完流程后,跳轉(zhuǎn)到首頁(yè)
        if語(yǔ)句的作用,是 判斷通過(guò)model返回?cái)?shù)據(jù)表的信息,如果為空則把微信用戶信息錄入到表中,再讀取出來(lái),存進(jìn)session。
        else 則數(shù)據(jù)表已經(jīng)存在該用戶,直接讀取,存進(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))
        {
           /*
         由于微信獲取到的用戶數(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)到用戶檢測(cè)中check_exists()去
        // echo $user_arr;exit();
        // var_dump($user_arr);
        return $user_arr;
      }else{
        //否則檢測(cè)cookie中是否存在該用戶,如果有,則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)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • PHP速成大法

    PHP速成大法

    本文主要是簡(jiǎn)單而全面的向大家介紹了一下php的相關(guān)知識(shí),php入門非常簡(jiǎn)單,稍微有點(diǎn)小基礎(chǔ)的小伙伴們一天就能入門。
    2015-01-01
  • Laravel 5.4中migrate報(bào)錯(cuò): Specified key was too long error的解決

    Laravel 5.4中migrate報(bào)錯(cuò): Specified key was too long error的解決

    今天在執(zhí)行l(wèi)aravel migrate時(shí)出現(xiàn)異常,找了好半天才找到問(wèn)題所在,所以這篇文章主要給大家介紹了關(guān)于Laravel 5.4中migrate報(bào)錯(cuò): Specified key was too long error的解決方法,需要的朋友可以參考下。
    2017-11-11
  • laravel使用組件實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)登入

    laravel使用組件實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)登入

    這篇文章主要介紹了laravel使用組件實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)登入,使用laravel組件 laravel-wechat調(diào)用,使用起來(lái)很方便,有需要的同學(xué)可以學(xué)習(xí)下
    2021-03-03
  • php使用Session和文件統(tǒng)計(jì)在線人數(shù)

    php使用Session和文件統(tǒng)計(jì)在線人數(shù)

    這篇文章主要介紹了php使用Session和文件統(tǒng)計(jì)在線人數(shù),本文直接給出實(shí)例代碼,需要的朋友可以參考下
    2015-07-07
  • ThinkPHP連接數(shù)據(jù)庫(kù)操作示例【基于DSN方式和數(shù)組傳參的方式】

    ThinkPHP連接數(shù)據(jù)庫(kù)操作示例【基于DSN方式和數(shù)組傳參的方式】

    這篇文章主要介紹了ThinkPHP連接數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式分析了thinkPHP基于DSN方式和數(shù)組傳參的方式進(jìn)行數(shù)據(jù)庫(kù)連接的實(shí)現(xiàn)步驟與屬性設(shè)置、控制器、模板使用等相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • php中的array_filter()函數(shù)的使用

    php中的array_filter()函數(shù)的使用

    php中的array_filter()函數(shù)用于篩選數(shù)組中的元素,并返回一個(gè)新的數(shù)組,新數(shù)組的元素是所有返回值為true的原數(shù)組元素,本文給大家介紹php中的array_filter()函數(shù)的使用,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • php遠(yuǎn)程請(qǐng)求CURL實(shí)例教程(爬蟲、保存登錄狀態(tài))

    php遠(yuǎn)程請(qǐng)求CURL實(shí)例教程(爬蟲、保存登錄狀態(tài))

    這篇文章主要給大家介紹了關(guān)于php遠(yuǎn)程請(qǐng)求CURL(爬蟲、保存登錄狀態(tài))的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 使用PHP開發(fā)留言板功能

    使用PHP開發(fā)留言板功能

    這篇文章主要介紹了使用PHP開發(fā)留言板功能,本文通過(guò)一段代碼給大家講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • php對(duì)數(shù)組排序的簡(jiǎn)單實(shí)例

    php對(duì)數(shù)組排序的簡(jiǎn)單實(shí)例

    分享一個(gè)php數(shù)組排序的例子,介紹了和php,有關(guān)的知識(shí)、技巧、經(jīng)驗(yàn),和一些php源碼等
    2013-12-12
  • ThinkPHP CURD方法之field方法詳解

    ThinkPHP CURD方法之field方法詳解

    ThinkPHP CURD方法的field方法主要目的是標(biāo)識(shí)要返回或者操作的字段,可以用于查詢和寫入操作。這篇文章主要介紹了field方法的詳細(xì)用法,需要的朋友可以參考下
    2014-06-06

最新評(píng)論

肥东县| 嘉黎县| 防城港市| 安多县| 丰台区| 方正县| 辛集市| 花垣县| 元谋县| 新巴尔虎右旗| 鄂托克旗| 海盐县| 阜阳市| 什邡市| 江津市| 扶风县| 沅陵县| 曲阜市| 林周县| 赤壁市| 江安县| 保山市| 区。| 乳源| 内丘县| 抚宁县| 尼玛县| 莱州市| 交城县| 和静县| 崇仁县| 承德市| 广安市| 满城县| 保康县| 舞阳县| 高台县| 景东| 繁昌县| 嘉义市| 屏山县|