Home > Backend Development > PHP Tutorial > 微信调试有关问题

微信调试有关问题

WBOY
Release: 2016-06-13 12:12:44
Original
1162 people have browsed it

微信调试问题
微信回复关键字  服务器接收    这里的逻辑怎么输出?? 比方说我收到了 xml数据 然后输出 但有时候我这里需要写个数组 然后输出 怎么做

如果写入文件和数据库表只能输出xml和字符串 不能输出数组  
------解决思路----------------------

<?php <br />define("TOKEN", "bjcy");#这里定义你留下的tooken<br />#这里是你定义好的消息回复<br />$default="";<br /><br />$address="http://www.bjcy07.com";<br />#这里是规则,判断用户发送过来的信息所包含的关键字,你可以定义更多,这里只是一个教程<br />$rule=Array("竞猜","竞猜送话费","送话费","竞猜白银");<br />#初始化<br />$Msg = new Msg();<br />$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];<br />$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);<br />$fromUsername = $postObj->FromUserName;<br />$toUsername = $postObj->ToUserName;<br />$keyword = trim($postObj->Content);<br />$key=$Msg->match($rule,$keyword);<br />#循环开始,判断用户所发送过来的消息,参数作用在下面的方法介绍里面<br />	switch ( $key )<br />	{<br /><br />		<br />		case "竞猜":<br />		$Msg->picMsg($fromUsername,$toUsername,"合创盈丰上线有礼:免费竞猜四位数,就中话费300元!","http://www.hcyf07.com/weixin/sdd2.jpg",'竞猜',"http://www.hcyf07.com");<br />		exit();<br />		<br />	<br />		<br />		case "竞猜送话费":<br />		$Msg->picMsg($fromUsername,$toUsername,"合创盈丰上线有礼:免费竞猜四位数,就中话费300元!","http://www.hcyf07.com/weixin/sdd2.jpg",'竞猜送话费',"http://www.hcyf07.com");<br />		exit();<br />		<br />		<br />		case "送话费":<br />		$Msg->picMsg($fromUsername,$toUsername,"合创盈丰上线有礼:免费竞猜四位数,就中话费300元!","http://www.hcyf07.com/weixin/sdd2.jpg",'送话费',"http://www.hcyf07.com");<br />		exit();<br />		<br />		case "竞猜白银":<br />		$Msg->picMsg($fromUsername,$toUsername,"合创盈丰上线有礼:免费竞猜四位数,就中话费300元!","http://www.hcyf07.com/weixin/sdd2.jpg",'竞猜白银',"http://www.hcyf07.com");<br />		exit();<br />		<br />		<br />		default:<br />		$Msg->txtMsg($fromUsername,$toUsername,$default);<br />	}<br /><br /><br />if(!empty( $keyword ))  <br />                {  <br />          <br />                $msgType = "text";  <br />        //入库试一下  <br />   <br />        //在这里可以判断接收到的keyword  <br />                      <br />                echo $resultStr;  <br />            }else{  <br />            if($event=="subscribe"){//首次关注  <br />                     $msgType = "text";  <br />                     $url = "http://www.bjcy07.com/";  <br />                     $contentStr = "您好,感谢关北京合创盈丰商品经营有限公司,北京合创盈丰商品经营有限公司是北京大宗商品交易所会员单位(会员席位号为007号),以现货白银等贵金属的销售、回购为主要经营项目,实行24小时买卖报价交易,同时提供贵金属投资咨询服务。".$url;  <br />                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  <br />                    echo  $resultStr;  <br />                      <br />                }  <br />                echo "Input something...";  <br />            }  <br /><br />/* class of msg */<br />class Msg<br />{ <br /> public function valid()<br />    {<br />        $echoStr = $_GET["echostr"];<br />        //valid signature , option<br />        if($this->checkSignature()){<br />         echo $echoStr;<br />         exit;<br />        }<br />    }<br />	#依次为 来源用户,发送到用户,时间,标题,图片地址,简介,点击微信消息时候的链接,当然你可以修改这个顺序<br />    function picMsg($fromUsername,$toUsername,$title,$PicUrl,$Discription,$Url)<br />    {<br />		//get post data, May be due to the different environments<br />		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];<br />			$time = time();<br />			$textTpl = "<xml> <br />			<ToUserName><![CDATA[%s]]></ToUserName> <br />			<FromUserName><![CDATA[%s]]></FromUserName> <br />			<CreateTime>%s</CreateTime> <br />			<MsgType><![CDATA[news]]></MsgType> <br />			<Content> </Content> <br />			<ArticleCount>1</ArticleCount> <br />			<Articles> <br />			<item><Title><![CDATA[%s]]></Title> <br />			<Discription><![CDATA[%s]]></Discription> <br />			<PicUrl><![CDATA[%s]]></PicUrl> <br />			<Url><![CDATA[%s]]></Url></item> <br />			</Articles> <br />			<FuncFlag>1</FuncFlag> <br />			</xml> ";    <br />				$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$title,$Discription,$PicUrl,$Url);<br />				echo $resultStr;<br />	}<br />		<br />	<br />	function txtMsg($fromUsername,$toUsername,$contentStr) <br />	{ <br />		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; <br />			$time = time(); <br />			$textTpl = "<xml> <br />			<ToUserName><![CDATA[%s]]></ToUserName> <br />			<FromUserName><![CDATA[%s]]></FromUserName> <br />			<CreateTime>%s</CreateTime> <br />			<MsgType><![CDATA[text]]></MsgType> <br />			<Content><![CDATA[%s]]></Content> <br />			<FuncFlag>0<FuncFlag> <br />			</xml>"; <br />			$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr); <br />			echo $resultStr; <br />			<br />	}<br />  <br />	private function checkSignature()<br />	{<br />		$signature = $_GET["signature"];<br />		$timestamp = $_GET["timestamp"];<br />		$nonce = $_GET["nonce"]; <br />		$token = TOKEN;<br />		$tmpArr = array($token, $timestamp, $nonce);<br />		sort($tmpArr);<br />		$tmpStr = implode( $tmpArr );<br />		$tmpStr = sha1( $tmpStr );<br /><br />		if( $tmpStr == $signature ){<br />		return true;<br />		}else{<br />			return false;<br />		}<br />	}<br />	#匹配用户关键字<br />	function match($rule,$keyword){ <br />		$num=count($rule)+1; <br />		for($i=0;$i<$num;$i++){ <br />			if( preg_match( "/$rule[$i]/",$keyword ) ){<br />				return $rule[$i];<br />				exit();<br />			}<br />		} <br />	} <br />	<br />} ?>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template