微信網頁受權獲取用戶信息(ThinkPHP5)+ 微信發送客服消息(一)

以thinkphp5爲實例,建立控制器php

class Kf extends Controller
{
    /**
     * [protected description]微信公衆號appid
     * @var [type]
     */
    protected $appid = "xxxxxxxxxxxxxxx";html

    /**
     * [protected description]微信公衆號appsecret
     * @var [type]
     */
    protected $appsecret = "xxxxxxxxxxxxxxxxx";算法

    /**
     * [protected description]微信公衆號回調地址
     * @var [type]
     */
    protected $redirect_uri = "https://xxxxx.xxxxxxx.cn/kf/getAccessToken?type=bw";thinkphp

    /**
     * [getAccessToken description]經過code獲取用戶信息(網頁受權方式)
     * @method getAccessToken
     * @return [type]         [description]
     */
    public function getInfo($appid,$appsecret,$code)
    {
        $urls = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid.
            "&secret=".$appsecret.
            "&code=".$code.
            "&grant_type=authorization_code";json

        $data = $this->http_curl($urls); //獲得用戶的access_token和openid
        $data = json_decode($data,true);
        return $data;
    }api

    /**
     * [getUserInfo description]獲取用戶信息
     * @method getUserInfo
     * @return [type]      [description]
     */
    public function getUserInfo($AccessToken, $OpenId)
    {
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$AccessToken."&openid=".$OpenId."&lang=zh_CN";
        $result = $this->http_curl($url);
        return $result;
    }微信

    /**
     * [getAccessToken description]主頁面, 回調獲取code
     * @method getAccessToken
     * @return [type]         [description]
     */
    public function getAccessToken(){
        //一、去微信獲取code 微信再重定向到當前接口
        if (!isset($_GET['type'])) {
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid.
                "&redirect_uri=".$this->redirect_uri.
                "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
            $this->redirect($url);
        }app

        $code = $_GET['code'];curl

        //2.微信重定向回來。 用code 獲取用戶信息。這兒就能獲取到code
        if(null != $code){thinkphp5

            $data = $this->getInfo($this->appid,$this->appsecret,$code);
            // var_dump($data);die;

            $UserInfo = $this->getUserInfo($data['access_token'], $data['openid']);
            $UserInfo = json_decode($UserInfo,true);
            // var_dump($UserInfo);die;

            //到此出爲止,能獲取到用戶的基本信息

            Session::set("wx.OpenId", $UserInfo['openid']);
            Session::set("wx.NickName", $UserInfo['nickname']);
            $this->redirect("https://xxxx.xxxxx.cn/kf/index?kf_access_token=".$UserInfo['openid']);

        }
    }

//業務頁面方法(html代碼見最後)    

public function index()
    {
        $openid = $_GET['kf_access_token'];
        if(null != $openid){
            return $this->fetch();
        }else{
            $this->error("OpenId系統錯誤 , 請聯繫公司客服");
        }
    }

//點擊發送微信客服消息 ,圖文消息   

public function SendInfo()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
        $res = $this->http_curl($url);
        $res = json_decode($res,true);
        // var_dump($res);die;
        $urls = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$res['access_token'];
        $nickname = Session::get('wx.NickName');
        $data = '{
            "touser":"'.Session::get('wx.OpenId').'",
            "msgtype":"text",
            "text":
            {
                 "content":"你們好我是測試消息~你應該是:'.$nickname.'"
            }
        }';
        $list = $this->http_curl2($urls,$data);
        var_dump($list);die;
    }

    //功能:curl通信
    private function http_curl ($url)
    {
        $ch = curl_init();//初始化
        curl_setopt($ch, CURLOPT_URL, $url);//初始化curl鏈接地址,設置要抓取的url
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設置將獲得的結果保存在字符串中仍是輸出在屏幕上,0爲直接輸出屏幕,非0則不輸出
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//對認證證書來源的檢查,false表示阻止檢查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//從證書中檢查ssl加密算法是否存在
        curl_setopt($ch,CURLINFO_HEADER_OUT,true);

        // curl_setopt ( $ch, CURLOPT_POST, 1 );
        // curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

        $result = curl_exec($ch);//執行請求獲取返回結果
        // $info = curl_getinfo($ch);
        curl_close($ch);//關閉curl會話
        return $result;
    }

    private function http_curl2 ($url,$data)
    {
        $ch = curl_init();//初始化
        curl_setopt($ch, CURLOPT_URL, $url);//初始化curl鏈接地址,設置要抓取的url
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設置將獲得的結果保存在字符串中仍是輸出在屏幕上,0爲直接輸出屏幕,非0則不輸出
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//對認證證書來源的檢查,false表示阻止檢查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//從證書中檢查ssl加密算法是否存在
        curl_setopt($ch,CURLINFO_HEADER_OUT,true);

        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

        $result = curl_exec($ch);//執行請求獲取返回結果
        // $info = curl_getinfo($ch);
        curl_close($ch);//關閉curl會話
        return $result;
    }

}

 

//html頁面代碼,很簡單的一個a標籤

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>

        //此處放置按鈕點擊發送客服消息(文字消息)         <a href="{:url('kf/SendInfo')}">發送消息</a>     </body> </html>

相關文章
相關標籤/搜索