企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
测试代码d1.php展示 ~~~ <?php define('TOKEN','zhangyouwu');//定义token $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; $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); $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,"没有这条文本信息(⊙o⊙)…"); } } } } private function receiveText($postSql){ $content=trim($postSql->Content); if(strstr($content,"你好")){//strstr模糊匹配 $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"); return $result; }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","zhangyouwu的微信开发","微信开发其实很简单","http://pic.baike.soso.com/ugc/baikepic2/5494/20170825195528-1233831972_jpg_387_310_14279.jpg/0","http://www.baidu.com"); return $result; } private function logger($content){//存储日志 $logSize=100000;//定义日志大小 $log="log.txt";//定义日志名字 if(file_exists($log)&&filesize($log)>$logSize){//如果日志存在并且大于定义的大小 unlink($log);//删除 } //文件写入1.名字2.内容,+时间防止重复3.内容往后加而不是覆盖 file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND); } } ?> ~~~ 测试图: ![](https://box.kancloud.cn/752aaa200c2d4c5ea9c085c7266a29df_328x552.png) 日志图: ![](https://box.kancloud.cn/613089796c51d58bf97d41906c59a5b5_1123x663.png)