宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

功能实现:

不管用户发送什么消息,都回复一条”谢谢您的关注”。

valid();

class Wechat{
	public function valid(){
		$echoStr = $_GET['echostr'];
		//如果是第一次接入
		if($this->checkSignature() && $echoStr ){
			echo $echoStr;
			exit;
		}else{
	    $this->responseMsg();
	   }
	}
	//校验方法
	private function checkSignature(){
		$signature = $_GET['signature'];
		$timestamp = $_GET['timestamp'];
		$nonce = $_GET['nonce'];
		$token = TOKEN;

		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode($tmpArr);
		$tmpStr = sha1($tmpStr);
		if($tmpStr == $signature){
			return true;
		}else{
			return false;
		}
	}
/* 普通文本消息

 
 
 1348831860
 
 
 1234567890123456
 
*/
	public function responseMsg(){
	    //获取微信服务器POST请求中的数据
	    
	    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
	    if( !empty($postStr) ){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);	
            $fromUser = $postObj->FromUserName;
            $toUser = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $time = time();
            
    	    $template = "
                         
                         
                         %s
                         
                         
                         ";
    	    if( !empty($keyword) ){
    	        $msgType = 'text';
    	        $content = "谢谢您的关注";
    	        $info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content);
    	        echo $info;
    	    }else{
    	        echo "请输入关键字";//消息为空
    	    }
    	}else{
    	    echo "";
    	    exit;
    	}
    }
}

登录后复制

以上就介绍了微信入坑第二弹,包括了微信,第二方面的内容,希望对PHP教程有兴趣的朋友有所帮助。