微信網頁開發常用功能封裝

<?php
namespace weixin;
class Weixin{
    public $_appid;
    public $_appsecret;
    public $_redirect_uri;
    public $_access_token;
    public $_jsapi_ticket;
    public function __construct ($appid = '', $appsecret = '', $redirect_uri = '') {
        $this->_appid = $appid ? $appid : Config::get('wxConfig.appid');//設置自己的appid
        $this->_appsecret = $appsecret ? $appsecret : Config::get('wxConfig.secret');//設置自己的app_secret
        $this->_redirect_uri = $redirect_uri ? $redirect_uri : Config::get('wxConfig.redirect_host');//設置自己的登錄回調地址
    }
    /*
     * 生成授權鏈接
     * @param $stat string 自定義參數
     * @param $scope string 授權方式
     * @return string
     */
    public function createAuthUrl ($state = '', $scope = 'snsapi_userinfo') {
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?';
        $url .= 'appid=' . $this->_appid . '&redirect_uri=' . urlencode($this->_redirect_uri);
        $url .= '&response_type=code&state=' . $state . '&scope=' . $scope . '#wechat_redirect';
        return $url;
    }


    //獲取換取用戶信息的access_token,openid
    public function getOpenid ($code) {
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
        $url .= 'appid=' . $this->_appid . '&secret=' . $this->_appsecret . '&code=' . $code . '&grant_type=authorization_code';
        $result = $this->getData($url, 'openid_accesstoken');
        return $result;
    }

    //獲取用戶信息
    public function getUserInfo ($code) {
        $data = $this->getOpenid($code);
        if ($data) {
            $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $data['access_token'] . '&openid=' . $data['openid'] . '&lang=zh_CN';
            $userInfo = $this->getData($url, 'user_info');
            return $userInfo;
        } else {
            return false;
        }
    }

    //http get 方法 默認返回數組
    private function httpGet ($url, $data_type = 'array') {
        $cl = curl_init();
        if (stripos($url, 'https://') !== FALSE) {
            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($cl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($cl, CURLOPT_URL, $url);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($cl);
        $status = curl_getinfo($cl);
        curl_close($cl);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            if ($data_type == 'json') {
                $content = json_decode($content);
            }
            return json_decode($content, true);
        } else {
            return FALSE;
        }
    }

    //http post 方法 默認返回數組
    private function httpPost ($url, $fields, $data_type = 'array') {
        $cl = curl_init();
        if (stripos($url, 'https://') !== FALSE) {
            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($cl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($cl, CURLOPT_URL, $url);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($cl, CURLOPT_POST, true);
        curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);
        $content = curl_exec($cl);
        $status = curl_getinfo($cl);
        curl_close($cl);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            if ($data_type == 'json') {
                $content = json_decode($content);
            }
            return json_decode($content, true);
        } else {
            return FALSE;
        }
    }

    //記錄日誌 根據框架情況或者講日誌保存到數據庫
    public function writeLog ($actionname, $errcode = '') {
        Log::write('微信公衆號錯誤日誌方法:' . $actionname . ' === CODE:' . $errcode, 'WXERRCODE');
        return true;
    }

    //獲取基本接口access_token
    public function getAccessToken () {
        $accessToken = Cache::get('access_token');//讀取緩存中的access_token
        if ($accessToken) {
            return $accessToken;
        } else {
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->_appid . '&secret=' . $this->_appsecret;
            $data = $this->getData($url, 'access_token');
            if ($data) {
                $accessToken = $data['access_token'];
                Cache::set('access_token', $accessToken, 7000);//根據實際情況緩存access_token,設置有效期
            } else {
                $accessToken = false;
            }
            return $accessToken;
        }
    }

    // 獲取 jssdk 簽名
    private function getJsapiTicket () {
        $tiket = Cache::get('jsapi_ticket');//讀取緩存中的jsapi_tiket
        if ($tiket) {
            return $tiket;
        } else {
            $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $this->getAccessToken() . '&type=jsapi';
            $data = $this->getData($url, 'jsapi_ticket');
            if (!$data) {
                $tiket = false;
            } else {
                $tiket = $data['ticket'];
                Cache::set('jsapi_ticket', $tiket, 7000);//設置 
            }
            return $tiket;
        }
    }

    /**
     * 獲取數據curl get
     * @param $url string 獲取鏈接
     * @param $input string 獲取字段
     * @return bool|mixed
     */
    private function getData ($url, $input) {
        $retry = 3;
        while ($retry--) {
            $data1 = $this->httpGet($url);
            if (!$data1) {
                continue;
            }
            break;
        }
        if (isset($data1['errcode']) && $data1['errcode']) {
            $this->writeLog($input, $data1['errcode']);
            return false;
        }
        return $data1;
    }

    protected function postData ($url, $field, $input) {
        $retry = 3;
        while ($retry--) {
            $data1 = $this->httpPost($url, $field);
            if (!$data1) {
                continue;
            }
            break;
        }
        if (isset($data1['errcode']) && $data1['errcode']) {
            $this->writeLog($input, $data1['errcode']);
            return false;
        }
        return $data1;
    }

    //生成隨機字符串
    public function createNonceStr ($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    //生成簽名
    public function creatSign () {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $jsapiTicket = $this->getJsapiTicket();
        if (!$jsapiTicket) {
            return false;
        }
        $timestamp = time();
        $nonceStr = $this->createNonceStr();
        // 這裏參數的順序要按照 key 值 ASCII 碼升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        $signature = sha1($string);
        $signPackage = array(
            "appId"     => $this->_appid,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return $signPackage;
    }

    // 發送模板消息
    /**
     * @param $touser string 用戶openid
     * @param $msg array 要發送的消息
     * @param $template_id string 模板消息ID
     * @param string $url 模板消息url
     * @param string $color 字體顏色
     * @return bool|string
     */
    public function sendTplMsg ($touser, $msg, $template_id, $url = '', $color = '#FF683F') {
        if (!$touser || !$template_id) {
            return false;
        }
        if (!is_array($msg) || !$msg) {
            return false;
        }
        $accessToken = $this->getAccessToken();
        if (!$accessToken) {
            return false;
        }
        $postUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $accessToken;
        $data = array(
            'touser'      => trim($touser),
            'template_id' => trim($template_id),
            'url'         => $url,
        );
        $message = array();
        $message['first'] = array(
            'value' => $msg['first'],
            'color' => $color
        );
        $message['keyword1'] = array(
            'value' => $msg['keyword1'],
            'color' => $color
        );
        $message['keyword2'] = array(
            'value' => $msg['keyword2'],
            'color' => $color
        );
        if (array_key_exists('keyword3', $msg)) {
            $message['keyword3'] = array(
                'value' => $msg['keyword3'],
                'color' => $color
            );
        }
        if (array_key_exists('keyword4', $msg)) {
            $message['keyword4'] = array(
                'value' => $msg['keyword4'],
                'color' => $color
            );
        }
        $message['remark'] = array(
            'value' => $msg['remark'],
            'color' => $color
        );
        $data['data'] = $message;
        $data = json_encode($data);
        $result = $this->httpPost($postUrl, $data);
        if (!$result) {
            return false;
        }
        if ($result['errcode'] != 0 || $result['errmsg'] != 'ok') {
            $this->writeLog('sendTplMsg', $result['errcode']);
            return false;
        }
        return true;
    }
}

完整代碼、使用方法:https://github.com/m249005779/wechat-common-functions 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章