闲的无聊造个轮子(简单微信操作类)

看看就好、初学者看看应该能懂个大概,会对微信开发有一个大概的概念

<?php

class Wx
{
    //微信openid
    private $openid;
    //微信appid
    private $appid;
    //微信appsecret
    private $appsecret;
    //微信accesstoken
    private $accessToken;
    //微信token
    private $token;

    //构造函数常用数据初始化
    public function __construct($data)
    {
        //验证
        if (isset($_GET['echostr'])) {
            $this->vaild();
        } else {
            $this->send();
        }


        //数据初始化
        $this->token = $data['token'];
        $this->appid = $data['appid'];
        $this->appsecret = $data['appsecret'];

        //获取accessToken
        $this->accessToken = $this->getAccessToken();
    }

    private function getAccessToken()
    {
        $appid = $this->appid;
        $appsecret = $this->appsecret;
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
        $res = json_decode($this->https_request($url), true);
        return $res['access_token'];
    }

    public function vaild()
    {
        if ($this->check()) {
            echo $_GET['echostr'];
            exit;
        }
    }

    public function check()
    {
        $signature = $_GET['signature'];
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET['nonce'];
        $token = $this->token;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }

    public function send()
    {
        $msgxml = file_get_contents("php://input");
        if (!empty($msgxml)) {
            $msgObj = simplexml_load_string($msgxml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $this->openid = $msgObj->FromUserName;
            $type = $msgObj->MsgType;
            $content = '';
            switch ($type) {
                case "event":
                    $content = $this->event($msgObj);
                    break;
                case "text":
                    $content = $this->text($msgObj);
                    break;
                case "image":
                    $content = '我是图片';
                    break;
                case "voice":
                    $content = '我是语音消息';
                    break;
                case "video":
                    $content = '我是视屏消息';
                    break;
                case "shortvideo":
                    $content = "我是短视频";
                    break;
                case "link":
                    $content = "我是链接消息";
                    break;
            }
            echo $content;
            exit;
        }
    }

    //事件处理函数
    public function event($obj)
    {
        $fromusername = $obj->FromUserName;
        $tousername = $obj->ToUserName;
        $time = time();
        $content = '';
        if ($obj->Event == 'subscribe') {
            $data = $this->getinfo();
            $nickname = $data['nickname'];
            $content = '欢迎' . $nickname . '关注我们';
        } elseif ($obj->Event == 'unsubscribe') {
            $content = '你会后悔的:(';
        } elseif ($obj->Event == 'LOCATION') {
//            $data = $this->getinfo();
//            $nickname = $data['nickname'];
//            $content = '欢迎' . $nickname . '关注我们';

        } elseif ($obj->Event == 'CLICK') {
            $content = $this->appsecret;
        } else {
            $content = '什么鬼???';
        }
        $tpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[text]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                    </xml>";
        $msg = sprintf($tpl, $fromusername, $tousername, $time, $content);
        return $msg;
    }

    /**
     * 文本处理函数
     */
    public function text($obj)
    {
        $fromusername = $obj->FromUserName;
        $tousername = $obj->ToUserName;
        $time = time();
        $content = 'dd';
        $tpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[text]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                    </xml>";
        $msg = sprintf($tpl, $fromusername, $tousername, $time, $content);
        return $msg;
    }

    /**
     * 关注得到用户信息
     */
    public function getinfo()
    {
        $openid = $this->openid;
        $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $this->accessToken . '&openid=' . $openid;
        $data = json_decode($this->https_request($url), true);
        return $data;
    }


    /**
     *创建微信菜单
     * @param data 菜单数据
     */
    public function createMuen()
    {

        $menu = '{
                "button": [
                    {
                        "name": "点击发送",
                        "sub_button": [
                            {
                                "type": "click",
                                "name": "点击推事件",
                                "key": "FANGBEI"
                            },
                            {
                                "name": "发送位置",
                                "type": "location_select",
                                "key": "rselfmenu_2_0"
                            }
                        ]
                    },
                    {
                        "name": "扫码发图",
                        "sub_button": [
                            {
                                "type": "scancode_waitmsg",
                                "name": "扫码带提示",
                                "key": "rselfmenu_0_0"
                            },
                            {
                                "type": "scancode_push",
                                "name": "扫码推事件",
                                "key": "rselfmenu_0_1"
                            },
                            {
                                "type": "pic_sysphoto",
                                "name": "系统拍照发图",
                                "key": "rselfmenu_1_0"
                            },
                            {
                                "type": "pic_photo_or_album",
                                "name": "拍照或相册发图",
                                "key": "rselfmenu_1_1"
                            },
                            {
                                "type": "pic_weixin",
                                "name": "微信相册发图",
                                "key": "rselfmenu_1_2"
                            }
                        ]
                    },
                    {
                        "type": "view",
                        "name": "��跳转网页",
                        "url": "http://quan.fangbei.org/m"
                    }
                ]
            }';
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$this->accessToken}";
        $this->https_request($url, $menu);
    }

    /**
     * @param $url
     * @param null $data post数据
     * @return mixed*
     */
    protected function https_request($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);
        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;
    }


}

 

转载于:https://my.oschina.net/raojiangjin/blog/1544239

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值