微信第三方平台授权实现公众号与粉丝交互

 public function __construct()
	{
		$param = model('parameter')->getval();
		$this->grant = new grant($param->token, $param->encodingAesKey, $param->appid, $param->appsecret);
		$this->redis = new redisChat();
		$this->clientkefu = new ClientKefu();
		$this->Gateway = new chat();
	}

    //微信公众平台URL以响应微信发送的Token验证
    public function wxOp()
    {
        $token = 'we*****';	//创建平台时填写的公众号消息校验Token
        $appId = '******';  //第三方平台appid
        $encodingAesKey = '*******';	//创建平台时填写的公众号消息加解密Key
		//写记事本
        $year = date('Y');
        $month = date('m');
        $day = date('d');
        $time = date('H:i:s');
        $route = dirname(dirname(dirname(__FILE__)));
        $path = "$route/notify/$year/$month";
		// 获取xml
        $xml = file_get_contents('php://input', 'r');
        libxml_disable_entity_loader(true);//转成php数组 禁止引用外部xml实体
        $data = json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA));
		//获取几个基本参数
        $timeStamp = empty($_GET['timestamp']) ? "" : trim($_GET['timestamp']);
        $nonce = empty($_GET['nonce']) ? "" : trim($_GET['nonce']);
        $msg_sign = empty($_GET['msg_signature']) ? "" : trim($_GET['msg_signature']);
		//接收XML数据
        $encryptMsg = file_get_contents('php://input');
        $pc = new wxBizMsgCrypt($token, $encodingAesKey, $appId);
        $xml_tree = new \DOMDocument();
        $xml_tree->loadXML($encryptMsg);
        $array_e = $xml_tree->getElementsByTagName('Encrypt');
        $encrypt = $array_e->item(0)->nodeValue;
        $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
        $from_xml = sprintf($format, $encrypt);
			//利用微信官方给的方法解密,$msg就是解密后的值
        $msg = '';
        $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
        $component_verify_ticket = "";
			//解密成功
        if ($errCode == 0) {
			//print("解密后: " . $msg . "\n");
            $xml = new \DOMDocument();
            $xml->loadXML($msg);
            $array_e = $xml->getElementsByTagName('ComponentVerifyTicket');
            $component_verify_ticket = $array_e->item(0)->nodeValue;
           	model('parameter')->saveval(['ticket' => $component_verify_ticket,'titime' => time()]);
        } else {			//解密失败
        }
        if (!file_exists($path)) {
            mkdir($path, 0777, true);
            $data = '04:' . date('Y-m-d H:i:s') . ':' . $errCode . ':' . $component_verify_ticket;
            file_put_contents("$path/$day.txt", "$data\r\n", FILE_APPEND);
        } else {
            $data = '04:' . date('Y-m-d H:i:s') . ':' . $errCode . ':' . $component_verify_ticket;
            file_put_contents("$path/$day.txt", "$data\r\n", FILE_APPEND);
        }
    }




    public function problemsOp()
    {
        $url = 'https://chat.wenidc.com/notify/wechat/problems';	//获取url
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                    //超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 1800);//在成功连接服务器前等待多久
                    //忽略证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//输出方式
        $data = curl_exec($ch);//返回值赋值给$data
        curl_close($ch);
        return;
        $path = dirname(dirname(dirname(__FILE__))) . '/notify/' . date('Y') . '/' . date('m');
        if (request()->ispost()) {
            $array = input('post.');
            $arr = json_encode($array);
            file_put_contents($path . '/' . date('d') . ".txt", "$arr\r\n", FILE_APPEND);
            $userid = model('wx_token')->where(['to_user_name' => $array['ToUserName']])->value('uid');
            $tongxingma = model('user')->where(['numid' => $userid])->value('token');
            $row = model('client')->where(['uid' => $array['FromUserName'], 'rulerid' => $userid])->find();
            file_put_contents($path . '/' . date('d') . ".txt", "查询数据库判断是否有值值为:\r\n", FILE_APPEND);
            $component_access_token = model('parameter')->getctoken();
            if ($array['MsgType'] == 'image') {
                $access_token = $this->grant->gettoken($array['ToUserName'], $component_access_token, $userid);
                $filename = $this->grant->getmedia($access_token, $array['MediaId'], UPLOADS_PATH); //获取接受到图片并下载下来,返回图片名
                file_put_contents($path . '/' . date('d') . ".txt", "下载的文件名$filename\r\n", FILE_APPEND);
                $array['message'] = $filename;
                if ($row != null && $row['status'] != 0) {
                    $this->sendMssage($row, $array, $tongxingma, 2);
                } else {
                    $this->addClient($component_access_token, $array, 2, $userid, $tongxingma);
                }
            } else if ($array['MsgType'] == 'text') {
                $message = $array['Content'];
                file_put_contents($path . '/' . date('d') . ".txt", "接受文本消息:$message\r\n", FILE_APPEND);
                $array['message'] = $array['Content'];
                if ($row != null && $row['status'] != 0) {
                    file_put_contents($path . '/' . date('d') . ".txt", "已接待后的消息\r\n", FILE_APPEND);
                    $this->sendMssage($row, $array, $tongxingma);
                } else {
                    file_put_contents($path . '/' . date('d') . ".txt", "未接待后的消息\r\n", FILE_APPEND);
                    $this->addClient($component_access_token, $array, 1, $userid, $tongxingma);
                }
            }
            if ($row != null && $row['server'] != null) {
                $this->clientkefu->horn($array['FromUserName'], $row['server'], $type = 3); //小喇叭
            } else {
                $this->clientkefu->horn($array['FromUserName'], '', $type = 3); //小喇叭
            }

        }
    }




	//通过该URL接收公众号或小程序消息和事件推送,该参数按规则填写
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值