铁雪资源网 Design By www.gsvan.com

本文实例讲述了基于CI框架的微信网页授权库。分享给大家供大家参考,具体如下:

这里演示建立在CI框架上的微信网页授权功能。

1. 微信小类库,网页授权放置在libraries文件夹

<"https://open.weixin.qq.com/connect/oauth2/authorize";
      return $url;
    }
    /* 通过code换取access_token*/
    function access_token($code)
    {
      /*获取到的code换取access_token和openid*/
      $post_url = "https://api.weixin.qq.com/sns/oauth2/access_token";
             // 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";
      $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";
      $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)"htmlcode">
<"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;
    }
}

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

标签:
CI框架,微信,网页授权库

铁雪资源网 Design By www.gsvan.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
铁雪资源网 Design By www.gsvan.com

评论“基于CI框架的微信网页授权库示例”

暂无基于CI框架的微信网页授权库示例的评论...