批改状态:合格
老师批语:
作业标题:0830作业
作业内容:1. 实现微信接入 2. 实现功能 当用户关注时 回复文本消息你好 3. 当用户在公众号中回复内容时,回复图片消息。 4. 当用户在公众号发送固定内容(只要内容中存在指定内容即可,未必全匹配)时,回复图文消息。 扩展:当用户发送指定内容时,随机发送一种类型消息


<?phpclass Message extends WeChat{function postText(){//判断微信服务请求的方法 get 还是 postif($_SERVER['REQUEST_METHOD'] == 'GET'){// //接入验证$this->checkSignature();}elseif($_SERVER['REQUEST_METHOD'] == 'POST'){//读取数据$xml = file_get_contents('php://input');if(!empty($xml)){//将xml数据转换成对象$postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);$toUser = $postObj->FromUserName;$formUser = $postObj->ToUserName;$time = time();//判断用户传入的类型 (文本 图文)if($postObj->MsgType == 'text' && preg_match('/\d/S',$postObj->Content)){$title = '真正成功了才是成功';$description = '进入php.cn主页';$picurl = 'https://www.php.cn/static/images/index_php_new4.jpg?1';$url = 'https://www.php.cn';$this->imgText($toUser,$formUser,$time,$title,$description,$picurl,$url);}elseif($postObj->MsgType == 'event'){if($postObj->Event == 'subscribe'){//制作对应的回复内容$content = '你好';$this->text($toUser,$formUser,$time,$content);}}}}}/** 制作文本消息*/private function text($toUser,$formUser,$time,$content){//制作对应的回复内容$templade = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";echo sprintf($templade,$toUser,$formUser,$time,$content);}/** 制作图文*/private function imgText($toUser,$fromUser,$time,$title,$description,$picurl,$url){$str="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></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>";echo sprintf($str,$toUser,$fromUser,$time,$title,$description,$picurl,$url);}}
3.当用户在公众号中回复内容时,回复图片消息。
代码
<?phpclass Message extends WeChat{function postText(){//判断微信服务请求的方法 get 还是 postif($_SERVER['REQUEST_METHOD'] == 'GET'){// //接入验证$this->checkSignature();}elseif($_SERVER['REQUEST_METHOD'] == 'POST'){//读取数据$xml = file_get_contents('php://input');if(!empty($xml)){//将xml数据转换成对象$postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);$toUser = $postObj->FromUserName;$formUser = $postObj->ToUserName;$time = time();//判断用户传入的类型 (文本 图文)if($postObj->MsgType == 'text' && preg_match('/\d/S',$postObj->Content)){$title = '真正成功了才是成功';$description = '进入php.cn主页';$picurl = 'https://www.php.cn/static/images/index_php_new4.jpg?1';$url = 'https://www.php.cn';$this->imgText($toUser,$formUser,$time,$title,$description,$picurl,$url);}elseif($postObj->MsgType == 'event'){if($postObj->Event == 'subscribe'){//制作对应的回复内容$content = '你好';$this->text($toUser,$formUser,$time,$content);}}}}}/** 制作文本消息*/private function text($toUser,$formUser,$time,$content){//制作对应的回复内容$templade = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";echo sprintf($templade,$toUser,$formUser,$time,$content);}/** 制作图文*/private function imgText($toUser,$fromUser,$time,$title,$description,$picurl,$url){$str="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></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>";echo sprintf($str,$toUser,$fromUser,$time,$title,$description,$picurl,$url);}}
当用户在公众号发送固定内容(只要内容中存在指定内容即可,未必全匹配)时,回复图文消息。 扩展:当用户发送指定内容时,随机发送一种类型消息
<?phpclass Message extends WeChat{function postText(){//判断微信服务请求的方法 get 还是 postif($_SERVER['REQUEST_METHOD'] == 'GET'){// //接入验证$this->checkSignature();}elseif($_SERVER['REQUEST_METHOD'] == 'POST'){//读取数据$xml = file_get_contents('php://input');if(!empty($xml)){//将xml数据转换成对象$postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);$toUser = $postObj->FromUserName;$formUser = $postObj->ToUserName;$time = time();//判断用户传入的类型 (文本 图文)if($postObj->MsgType == 'text' && preg_match('/\d/S',$postObj->Content)){$title = '真正成功了才是成功';$description = '进入php.cn主页';$picurl = 'https://www.php.cn/static/images/index_php_new4.jpg?1';$url = 'https://www.php.cn';$this->imgText($toUser,$formUser,$time,$title,$description,$picurl,$url);}elseif($postObj->MsgType == 'event'){if($postObj->Event == 'subscribe'){//制作对应的回复内容$content = '你好';$this->text($toUser,$formUser,$time,$content);}}}}}/** 制作文本消息*/private function text($toUser,$formUser,$time,$content){//制作对应的回复内容$templade = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";echo sprintf($templade,$toUser,$formUser,$time,$content);}/** 制作图文*/private function imgText($toUser,$fromUser,$time,$title,$description,$picurl,$url){$str="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></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>";echo sprintf($str,$toUser,$fromUser,$time,$title,$description,$picurl,$url);}}
匹配数字回复图文消息
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号