微信开发(PHP实现订阅号的公众号配置和自动回复)

    首先在微信公众平台(网址:https://mp.weixin.qq.com)申请一个订阅号,然后在开发里找到开发者工具点击公众平台测试账号,在测试账号内进行微信开发实验。
    1. 设置一个自己的有效的域名网址和TOKEN(就是暗号),TOKEN一定要与PHP代码中的TOKEN验证一致否则会一直配置失败( 写有这段代码的文件一定要传到有效的域名网址内。与设置的网址必须相同)。
    下面是PHP代码(在微信公众平台开发里的开发者文档内有部分代码) 。
run();

class wechatCallbackapiTest
{
	public function run()
    {

    	if ($this->checkSignature()==false) {
    		die('非法请求');
    	}

    	if (isset($_GET["echostr"])) {
    		$echostr = $_GET["echostr"];
    		echo $echostr;
    		exit();
    	} else {
    		$this->responseMsg();
    	}
    	

        /*$echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;*/
        }
    }

    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
		if (!empty($postStr)){
                
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "
							
							
							%s
							
							
							0
							";             
				if(!empty( $keyword ))
                {
              		$msgType = "text";
                	$contentStr = "Welcome to wechat world!";
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                	echo $resultStr;
                }else{
                	echo "Input something...";
                }

        }else {
        	echo "";
        	exit;
        }
    }
		
	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;
		}
	}
}

?>

2.自动回复 
首先重新写一个方法在上面那个文件夹里,再调用此方法,注意不能重名。
以下代码是给订阅号发天气,它会回复天气晴朗,发放假,它会回复你“不好好奋斗瞎想啥?”回复其他则会回复"Welcome to wechat world!"
public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		file_put_contents('msg.txt', $postStr,FILE_APPEND);
      	//extract post data
		if (!empty($postStr)){
                
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                //$msgType = $postObj->MsgType;//消息类型  
                //$event = $postObj->Event;//时间类型,subscribe(订阅)、unsubscribe(取消订阅)
                $textTpl = "
							
							
							%s
							
							
							0
							";             
				if(!empty( $keyword ))
                {
              		$msgType = "text";
              		$contentStr = $this->autohuifu($keyword);
                	//$contentStr = "Welcome to wechat world!";
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                	echo $resultStr;
                }else{
                	echo "Input something...";
                }

        }else {
        	echo "";
        	exit;
        }
    }
	

    public function autohuifu($keyword){
		
		if($keyword =='天气'){
			$contentStr = "天气晴朗";
                	
        	}else if($keyword =='放假'){
        	$contentStr = "成天不好好奋斗瞎想啥?";

        }else{
        	$contentStr = "Welcome to wechat world!";
        
        }

        return $contentStr;
    


 
  

你可能感兴趣的:(微信开发(PHP实现订阅号的公众号配置和自动回复))