CI-微信公众号获取openid和消息推送

<?php
/**
 * Created by PhpStorm.
 * User: dbag
 */
class Wx_model extends CI_Model{
    public $APPID = '';
    public $APPSECRET = '';
    function __construct(){
        $this->load->database();
    }
    /*
     * 用户授权登录
     */
    function userLogin(){
        $redirect_uri = urlencode('http://****/wxOpenid'); //跳转到下面调用这个方法的地址
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->APPID.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
        header('Location:'.$url);
    }
    /*
     * code获取openid
     */
    function CodegetOpenid($code){
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->APPID.'&secret='.$this->APPSECRET.'&code='.$code.'&grant_type=authorization_code';
        $res = file_get_contents($url);
        $res = json_decode($res,true);
        if($res['openid']){
            return $res['openid'];
        }else{
            return false;
        }

    }

    /**
     * 生成token 每2个生成一次 脚本执行
     *
     */
    function access_token(){
        $url  = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->APPID.'&secret='.$this->APPSECRET;
        $res = file_get_contents($url);
        $res = json_decode($res,true);
        if($res['access_token']){
            file_put_contents('access_token.json', $res['access_token']);
        }else{
            $error = file_get_contents('access_token_error.json');
            file_put_contents('access_token_error.json', $error.'获取token失败'.date('Y-m-d H:i:s',time())."\r\n");
        }
    }
    /**
     * 获取token
     */
    function gain_token(){
        $token = file_get_contents('access_token.json');
        if($token){
            return ['code'=>0,'token'=>$token];
        }else{
            return ['code'=>1,'msg'=>'获取失败'];
        }
    }

    /**
     * 微信消息推送
     * @param $openid 用户opendID
     * @param $url 跳转地址
     * @param $title 推送标题
     * @param $realname 用户姓名
     * @param $starttime 开始时间
     * @param $content 推送内容
     * @param string $remarks 备注
     * @return $arr;
     */
    function postNews($openid,$url,$title,$realname,$starttime,$content,$remarks=''){
        $token = $this->gain_token();
        $wx_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token['token'];
        $data = [
            'touser'=>$openid,
            'template_id'=>'-dq0w-Orz0XRcQvvdse-BdNlwny1qU5i24gePpRsE6s',
            'url'=>$url,
            'topcolor'=>'#FF0000',
            'data'=>[
                'first'=>[
                    'value'=>$title,
                    'color'=>'#173177'
                ],
                'keyword1'=>[
                    'value'=>$realname,
                    'color'=>'#173177'
                ],
                'keyword2'=>[
                    'value'=>$starttime,
                    'color'=>'#173177'
                ],
                'keyword3'=>[
                    'value'=>$content,
                    'color'=>'#173177'
                ],
                'remark'=>[
                    'value'=>$remarks,
                    'color'=>'#173177'
                ]
            ]
        ];
       $res =  $this->https_post($wx_url,json_encode($data));
       return $res;

    }

    /**
     * @param $url
     * @param null $data
     * @return mixed
     */
    function https_post($url, $data = null){
        $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_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
        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);
        return $output;
    }

}

//CI 控制器调用方法
/*
* 微信导航跳转地址
*/
function wxLogin(){
$this->load->model('Wx_model');
$this->Wx_model->userLogin();
}

function wxOpenid(){
$this->load->model('Wx_model');
$openid = $this->Wx_model->CodegetOpenid($_GET['code']);
if($openid){
//查询数据库中是否存在
//不存在携带ID跳转登录 登录后绑定该ID

//存在获取信息直接登录

}else{
//未获取到
//走正常登录
}
}
//消息推送

$this->load->model('Wx_model');

$this->Wx_model->postNews('用户opendID','跳转地址URL','推送标题','用户姓名','开始时间','推送内容','备注');



 

posted @ 2018-05-07 18:21  大智如蠢  阅读(1217)  评论(0编辑  收藏  举报