微信回复图文和多图文


 

<?phpdefine('TOKEN','yida');$obj=new Weixin();if(!isset($_GET['echostr'])){$obj->receive();//获取收到信息}else{$obj->checkSignature();}class Weixin{//验证通信public function checkSignature() { $signature = $_GET["signature"]; //加密签名 $timestamp = $_GET["timestamp"]; //时间戳 $nonce = $_GET["nonce"]; //随机数$token = TOKEN; //token$tmpArr = array($token, $timestamp, $nonce);//组成新数组sort($tmpArr, SORT_STRING);//重新排序$tmpStr = implode( $tmpArr );//转换成字符串$tmpStr = sha1( $tmpStr ); //再将字符串进行加密if( $tmpStr == $signature ){echo $_GET['echostr'];}else{return false;} } public function receive(){ $obj=$GLOBALS['HTTP_RAW_POST_DATA'];//获取返回信息 $postSql=simplexml_load_string($obj,'SimpleXMLElement',LIBXML_NOCDATA);//把xml转换成对象 $this->logger("接受:\n".$obj); if(!empty($postSql)){ switch(trim($postSql->MsgType)){//匹配类型 case "text" : $result=$this->receiveText($postSql); if(!empty($result)){ echo $result; }else{ $xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content> </xml>";echo $result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),$postSql->MsgType,"没有这条文本消息"); } } } }//回复图文信息 private function receiveText($postSql){ $content=trim($postSql->Content); if(strstr($content,"你好")){ $xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content> </xml>";$result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),$postSql->MsgType,"hello"); }else if(strstr($content,"单图文")){ $result=$this->receiveImage($postSql); } $this->logger("发送单图文消息:\n".$result); return $result; } private function receiveImage($postSql){ $xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item></Articles></xml> ";$result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),"news","跟益达学微信开发,教益达泡妹子","它就是中国海南海花岛——缤纷水上乐园23项游乐打造雪山滑道区、激流河道区巨浪演绎区、阖家欢乐区极限滑道区五大主题游乐区","http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");return $result; }//多图文
private function   receiveImages($postSql){
    			$content=array();
    			$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");


    			$str="<item>
				<Title><![CDATA[%s]]></Title>
				<Description><![CDATA[%s]]></Description>
				<PicUrl><![CDATA[%s]]></PicUrl>
				<Url><![CDATA[%s]]></Url>
				</item>";

				$news="";
				foreach ($content as $newArray) {
					$news.=sprintf($str,$newArray['Title'],$newArray['Description'],$newArray['PicUrl'],$newArray['Url']);
				}

    			$xml="<xml>
				<ToUserName><![CDATA[%s]]></ToUserName>
				<FromUserName><![CDATA[%s]]></FromUserName>
				<CreateTime>%s</CreateTime>
				<MsgType><![CDATA[%s]]></MsgType>
				<ArticleCount>%s</ArticleCount>
				<Articles>
					$news
				</Articles>
				</xml> ";

					$result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),"news",count($content));

					return $result;


    }


 //写日志 private function logger($content){ $logSize=100000; $log="log.txt"; if(file_exists($log) && filesize($log) > $logSize){ unlink($log); } file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND); }} ?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值