CI-WeChat official account to obtain openid and message push

<?php
/**
 * Created by PhpStorm.
 * User: dbag
 */
class Wx_model extends CI_Model{
    public $APPID = '';
    public $APPSECRET = '';
    function __construct(){
        $this->load->database();
    }
    /*
     * User authorized login
     */ 
    function userLogin(){
         $redirect_uri = urlencode ('http://****/wxOpenid' ); //Jump to the address where this method is called below
         $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 to get 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;
        }

    }

    /**
     * Generate token every 2 times to generate script execution
     *
     */
    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");
        }
    }
    /**
     * Get token
     */
    function gain_token(){
        $token = file_get_contents('access_token.json');
        if($token){
            return ['code'=>0,'token'=>$token];
        } else {
             return ['code'=>1,'msg'=>'Get failed' ];
        }
    }

    /**
     * WeChat message push
     * @param $openid user opendID
     * @param $url jump address
     * @param $title push title
     * @param $realname Username
     * @param $starttime start time
     * @param $content push content
     * @param string $remarks 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 controller call method
/* 
* Wechat navigation jump address
*/
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){
//Query whether the ID exists in the database
//There is no carry ID Bind the ID after jumping to log in and log in

//There is information to log in directly

}else{
//Not obtained
//Go to normal login
}
}
//Message push

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

$this->Wx_model->postNews('user openedID','jump address URL','push title','user name','start time','push content','remarks');



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325767531&siteId=291194637