微信发送模板消息为什么手机上会受到二十几条消息

移动开发 码拜 8年前 (2016-03-31) 1231次浏览
如题,代码如下。首次用手机微信关注时,会收到正确的模板消息,但是会收到二十几条,这是为什么呀?求指导答,谢谢

<?php
//发送模板消息
class class_weixin
{

	public function valid()
    {
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	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;
		}
	}

public function send_template_message($data)
{
	$access_token="TB51ZSi_tr0LqkaC6tbPycuWJzmBLs-MZfbL4-bNisM4kmLKivYITlN628mMocaugWJPJzUYaPBKqKHxI7vYk_TZ2q4-3PEx6IPJB9ceOfVgUGiQ-XGOqpRs9XIxDMIfADAVEA";
	$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token ";
    var_dump($url);//var_dump函数可以直接输出一个变量的值
	$res=$this->http_request($url,$data);
	return json_decode($res,true);
}
//https请求
protected function http_request($url,$data=NULL)
{
	$ch=curl_init();//初始化一个curl会话
	curl_setopt($ch, CURLOPT_URL, $url);//设置curl的参数
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	if(!empty($data)){
	  curl_setopt($ch,CURLOPT_POST,1);
	  curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
	}
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output=curl_exec($ch);//执行一个curl会话
	curl_close($ch);
	return $output;
}
    
}
$template=array(
                "touser"=>"ovx4Xw8ppIX5ZcsZNP7DtdD5rcGE",
		"template_id"=>"VCc3eWTJR0Z5k0Z5slofgNAlAAtveW9LOMr1yEjM",
                //"url"=>"http://weixin.qq.com/",
		"topcolor"=>"#FF0000",
		"data"=>array("first"=>array("value"=>urlencode("您好!"),
				                     "color"=>"",
				                    ),
		              "keyword1"=>array("value"=>urlencode("您有一项消息"),
		              		           "color"=>"",
		                              ),
				      "keyword2"=>array("value"=>urlencode("2016年11月19日"),
				      		         "color"=>"",
				                    ),
				      "keyword3"=>array("value"=>urlencode("北京"),
				      		        "color"=>"",
				                    ),
				      "remark"=>array("value"=>urlencode("\n要快哦!"),
				      		         "color"=>"",
				                    ),
		            )


);
//调用SDK发送
//require_once("weixin.class.php");
define("TOKEN", "weixin");
$weixin = new class_weixin();
//$weixin->valid();
//var_dump($weixin->send_template_message(urldecode(json_encode($template))));
$weixin->send_template_message(urldecode(json_encode($template)));
?>
解决方案

20

原因是微信服务器还会给你返回消息,假如是这个地址的话,就会造成死循环!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明微信发送模板消息为什么手机上会受到二十几条消息
喜欢 (0)
[1034331897@qq.com]
分享 (0)