搜索
首页后端开发php教程微信公众平台开发入门-PHP,实现自动恢复文本,图文,点击事件

微信公众平台开发入门--PHP,实现自动回复文本,图文,点击事件

一页代码实现微信基本回复和点击事件功能,部署上去sae或者bae,妥妥的基本免费的服务器

不懂代码都基本每个人都可以做自己的微信公众号了羡慕


<?phpdefine ("TOKEN", "mzh");        //换成你的token$wechatObj = new wechatCallbackapiTest();if (isset($_GET['echostr'])) {     //验证微信    $wechatObj->valid();}else{                     //回复消息    $wechatObj->responseMsg();}class wechatCallbackapiTest{    public function valid()    {        $echoStr = $_GET["echostr"];        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 responseMsg()	{    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];    if (!empty($postStr)){        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);        $RX_TYPE = trim($postObj->MsgType);        switch ($RX_TYPE)        {            case "text":                $resultStr = $this->receiveText($postObj);                break;            case "image":                $resultStr = $this->receiveImage($postObj);                break;            case "location":                $resultStr = $this->receiveLocation($postObj);                break;            case "voice":                $resultStr = $this->receiveVoice($postObj);                break;            case "video":                $resultStr = $this->receiveVideo($postObj);                break;            case "link":                $resultStr = $this->receiveLink($postObj);                break;            case "event":                $resultStr = $this->receiveEvent($postObj);                break;            default:                $resultStr = "unknow msg type: ".$RX_TYPE;                break;        }        echo $resultStr;    }else {        echo "";        exit;    }	}        //接收文本消息    private function receiveText($object)    {        $keyword = trim($object->Content);        $url = "http://api100.duapp.com/movie/?appkey=DIY_miaomiao&name=".$keyword;        $output = file_get_contents($url,$keyword);        $contentStr = json_decode($output, true);        if (is_array($contentStr)){            $resultStr = $this->transmitNews($object, $contentStr);        }else{            $resultStr = $this->transmitText($object, $contentStr);        }        return $resultStr;    }        //接收事件,关注等    private function receiveEvent($object)    {        $contentStr = "";        switch ($object->Event)        {            case "subscribe":                $contentStr = "你关注了我";    //关注后回复内容                break;            case "unsubscribe":                $contentStr = "";                break;            case "CLICK":                $contentStr =  $this->receiveClick($object);    //点击事件                break;            default:                $contentStr = "receive a new event: ".$object->Event;                break;        }                return $contentStr;    }        //接收图片    private function receiveImage($object)    {        $contentStr = "你发送的是图片,地址为:".$object->PicUrl;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }            //接收语音    private function receiveVoice($object)    {        $contentStr = "你发送的是语音,媒体ID为:".$object->MediaId;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }        //接收视频    private function receiveVideo($object)    {        $contentStr = "你发送的是视频,媒体ID为:".$object->MediaId;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }        //位置消息    private function receiveLocation($object)    {        $contentStr = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }        //链接消息    private function receiveLink($object)    {        $contentStr = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }          <p> //点击菜单消息    private function receiveClick($object)    {         switch ($object->EventKey)         {             case "1":             $contentStr = "猫咪酱个性DIY服装,我们专业定制个性【班服,情侣装,亲子装等,有长短T恤,卫衣,长短裤】 来图印制即可,给你温馨可爱的TA,有事可直接留言微信";             break;                          case "2":             $contentStr = "你点击了菜单: ".$object->EventKey;             break;                          case "3":             $contentStr = "是傻逼";             break;                          default:             $contentStr = "你点击了菜单: ".$object->EventKey;             break;         }                        //两种回复        if (is_array($contentStr)){            $resultStr = $this->transmitNews($object, $contentStr);        }else{            $resultStr = $this->transmitText($object, $contentStr);        }        return  $resultStr;    }                                                        //回复文本消息    private function transmitText($object, $content)    {        $textTpl = "<xml>        <tousername></tousername>        <fromusername></fromusername>        <createtime>%s</createtime>        <msgtype></msgtype>        <content></content>        </xml>";        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);        return $resultStr;    }</p><p>                        //回复图文    private function transmitNews($object, $arr_item)    {        if(!is_array($arr_item))            return;</p><p>        $itemTpl = "    <item>        <title></title>        <description></description>        <picurl></picurl>        <url></url>     </item>";        $item_str = "";        foreach ($arr_item as $item)            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);</p><p>        $newsTpl = "<xml>        <tousername></tousername>        <fromusername></fromusername>        <createtime>%s</createtime>        <msgtype></msgtype>        <content></content>        <articlecount>%s</articlecount>        <articles>        $item_str</articles>        </xml>";</p><p>        $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));        return $resultStr;    }            //音乐消息    private function transmitMusic($object, $musicArray, $flag = 0)    {        $itemTpl = "<music>        <title></title>        <description></description>        <musicurl></musicurl>        <hqmusicurl></hqmusicurl>        </music>";</p><p>        $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);</p><p>        $textTpl = "<xml>        <tousername></tousername>        <fromusername></fromusername>        <createtime>%s</createtime>        <msgtype></msgtype>        $item_str        <funcflag>%d</funcflag>        </xml>";</p><p>        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $flag);        return $resultStr;    }            }?></p>
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

如何使PHP应用程序更快如何使PHP应用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能优化清单:立即提高速度PHP性能优化清单:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

PHP依赖注入:提高代码可检验性PHP依赖注入:提高代码可检验性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能优化:数据库查询优化PHP性能优化:数据库查询优化May 12, 2025 am 12:02 AM

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。