微信网页授权获取用户信息(ThinkPHP5)+ 微信发送客服消息(一)

以thinkphp5为实例,创建控制器

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

    /**
     * [protected description]微信公众号appsecret
     * @var [type]
     */
    protected $appsecret = "xxxxxxxxxxxxxxxxx";

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

    /**
     * [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";

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

    /**
     * [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(){
        //1、去微信获取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);
        }

        $code = $_GET['code'];

        //2.微信重定向回来。 用code 获取用户信息。这儿就能获取到code
        if(null != $code){

            $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>

转载于:https://my.oschina.net/zfblog/blog/3028315

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值