对微信验证进行封装,以面向对象的方式进行访问

index.php

<?php
include './common.php';
//因为define的常量是全局的,所以放在该文件也是可以的
define('TOKEN','weixin');
$wechat = new weChat();

//如果echostr 存在则需要验证第三方服务器的真实性
if($_GET["echostr"]){
	$wechat->valid();
}else{
	$wechat->responseMeg();
}

common.php类

<?php

class weChat
{
	//验证消息,检验签名是否成功
	public function valid()
	{
		$echostr = $_GET["echostr"];
		if($this->checkSignature()){
			echo "$echostr";
		}else{
			echo 'error';
			exit;
		}
	}

	//检验签名
	public function checkSignature()
	{
		//第三方服务器与微信服务器进行消息接入,
		// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html

		// 接收微信服务器发送的四个验证参数
		//加密签名
		        $signature = $_GET["signature"];
		//时间戳
		        $timestamp = $_GET["timestamp"];
		//随机数
		        $nonce = $_GET["nonce"];
		//随机字符串
		        $echostr = $_GET["echostr"];
		// TOKEN
		        // define('TOKEN','weixin');

		//1.进行字典序排序
		        $tmpArr = array(TOKEN, $timestamp, $nonce);
		    sort($tmpArr, SORT_STRING);
		//2.将三个参数字符串拼接成一个字符串进行sha1加密 
		        $tmpStr = implode( $tmpArr );
		    $tmpStr = sha1( $tmpStr );
		//3.开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
		        if( $tmpStr == $signature ){
		            // echo "$echostr";
		            return true;
		        }else{
		            return false;
		        }

	}


	//处理用户请求消息
	public function responseMeg()
	{
		//服务器接收微信POST发送的xml格式原生数据
	    // $postStr  = $GLOBALS['HTTP_RAW_POST_DATA'];
	        $postStr = file_get_contents("php://input");

	    if (!$postStr) {
	        echo "post data error";
	        exit;
	    }

		//将xml格式转对象形式输出  LIBXML_NOCDATA - 将 CDATA 设置为文本节点
		$postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
		//处理消息的类型
		$MsgType = $postObj->MsgType;
		//根据xml对象与消息类型处理
		$this->checkMsgType($postObj,$MsgType);
	}

	//处理消息类型
	public function checkMsgType($postObj,$MsgType)
	{
		switch ($MsgType) {
			case 'text':
				//处理文本消息
				$this->receiveText($postObj);
				break;
			case 'image':
				
				break;
			default:
				
				break;
		}
	}

	//处理文本消息
	public function receiveText($postObj)
	{
		$Content = $postObj->Content;
		switch ($Content) {
			case '点歌':
				$this->replyText($postObj,'123123');
				break;
			case '笑话':
				$this->replyText($postObj,'xiaohua ing');
				break;
			default:
				# code...
				break;
		}
	}

	//回复文本消息
	public function replyText($postObj,$Content)
	{
		$xml = '<xml>
                  <ToUserName><![CDATA[%s]]></ToUserName>
                  <FromUserName><![CDATA[%s]]></FromUserName>
                  <CreateTime>%d</CreateTime>
                  <MsgType><![CDATA[text]]></MsgType>
                  <Content><![CDATA[%s]]></Content>
                </xml>';
        echo sprintf($xml,$postObj->FromUserName,$postObj->ToUserName,time(),$Content);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值