- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章php实现基于微信公众平台开发SDK(demo)扩展的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了php实现基于微信公众平台开发SDK(demo)扩展的方法。分享给大家供大家参考。具体分析如下:
该扩展基于官方的微信公众平台SDK,这里只做了简单地封装,实现了一些基本的功能(如天气查询,翻译,自动聊天机器人,自定义菜单接口)仅供学习之用.代码如下
define("TOKEN", "xingans"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsg(); class wechatCallbackapiTest { //签名验证公共接口 public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } //主入口处理函数 public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!emptyempty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $msgType=trim($postObj->MsgType); switch($msgType){ case 'text': $resultStr=$this->handleText($postObj); break; case 'event': $resultStr=$this->handleEvent($postObj); break; default: $resultStr=$this->handleDefault($postObj); break; } echo $resultStr; }else { echo "Error"; exit; } } //处理文本消息 private function handleText($obj){ $keyword=trim($obj->Content); if(preg_match('/天气/',$keyword)){ $contentStr=$this->handleWeather($obj); }elseif(preg_match('/翻译/',$keyword)){ $contentStr=$this->handleTranslation($obj); }else{ $contentStr=$this->handleChat($obj); } return $this->handleStr($obj,$contentStr); } //处理天气 private function handleWeather($obj){ $keyword=mb_substr($obj->Content,-2,2,'utf-8'); $zone=mb_substr($obj->Content,0,-2,'utf-8'); if($keyword=='天气' && !emptyempty($zone)){ $zoneArr=json_decode(file_get_contents('http://api.k780.com:88/?app=weather.city&format=json'),true); $zoneArr=$zoneArr['result']; $cityId=''; foreach($zoneArr as $value){ if($zone==$value['citynm']){ $cityId=$value['weaid']; break; } } if(!emptyempty($cityId)){ $data=file_get_contents("http://api.k780.com:88/?app=weather.today&weaid=$cityId&appkey=10638&sign=3736578f099375665f9f141a6326b757&format=json"); $data=json_decode($data); $contentStr="今天是:".$data->result->days.",".$data->result-> week.",".$data->result->citynm."天气:".$data->result->weather."n温度:".$data->result->temperature.", ".$data->result->wind_direction.",".$data->result->wind_power.", 最低温度:".$data->result->temp_low.",最高温度:".$data->result->temp_high; }else{ $contentStr='找不到输入的城市!'; } }else{ $contentStr='输入的查询格式不正确!'; } return $contentStr; } //处理翻译 private function handleTranslation($obj){ $keyword=mb_substr($obj->Content,0,2,'utf-8'); $words=mb_substr($obj->Content,2,220,'utf-8'); if($keyword=='翻译' && !emptyempty($words)){ $data=file_get_contents('http://fanyi.youdao.com/openapi.do?keyfrom=zfsblog&key=364295447&type=data&doctype=json&version=1.1&q='.urlencode($words)); $data=json_decode($data,true); switch($data['errorCode']){ case '0': $contentStr=$data['translation'][0]; break; case '20': $contentStr='要翻译的文本过长'; break; case '30': $contentStr='无法进行有效的翻译'; break; case '40': $contentStr='不支持的语言类型'; break; case '50': $contentStr='无效的key'; break; default: $contentStr='Error'; break; } }else{ $contentStr='输入的翻译格式不正确!'; } return $contentStr; } //处理聊天信息 private function handleChat($obj){ $keywords=$obj->Content; $curlPost=array("chat"=>$keywords); $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');//抓取指定网页 curl_setopt($ch, CURLOPT_HEADER, 0);//设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//运行curl curl_close($ch); if(!emptyempty($data)){ $contentStr=$data; }else{ $ran=rand(1,5); switch($ran){ case 1: $contentStr= "小九今天累了,明天再陪你聊天吧"; break; case 2: $contentStr= "小九睡觉喽~~"; break; case 3: $contentStr= "呼呼~~呼呼~~"; break; case 4: $contentStr= "你话好多啊,不跟你聊了"; break; case 5: $contentStr= "你话好多啊,不跟你聊了"; break; } } return $contentStr; } //创建自定义菜单 public function createMenu($data,$token){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$token); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); return $result; } //查询自定义菜单 function getMenu($token){ $url="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=$token"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true) ; //获取数据返回 curl_setopt($ch, CURLOPT_BINARYTRANSFER,true) ; //在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false); return $output = curl_exec($ch); } //删除自定义菜单 public function deleteMenu($token){ $url="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$token; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $result = curl_exec($ch); curl_close($ch); return $result; } 。
。
//处理事件消息 private function handleEvent($obj){ $content=''; switch($obj->Event){ case 'subscribe': $content.="welcome-欢迎关注该公众号号!"; break; case 'unsubscribe': $content.="感谢您一直以来对该公众号的关注,再见!"; break; default: $content.=""; break; } return $this->handleStr($obj,$content); } //处理回复消息字符串 private function handleStr($obj,$content='',$flag=0){ $textTpl = ""; return sprintf($textTpl, $obj->FromUserName, $obj->ToUserName, time(), $content,$flag); } //签名验证函数 private 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 ){ return true; }else{ return false; } } } 。
。
希望本文所述对大家的php程序设计有所帮助.
最后此篇关于php实现基于微信公众平台开发SDK(demo)扩展的方法的文章就讲到这里了,如果你想了解更多关于php实现基于微信公众平台开发SDK(demo)扩展的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我在 cordova@7.1.0、cordova-ios@4.5.2 下运行。安装平台:ios 4.5.2。 我运行 npm install、bower install,然后运行 cordova
我正在使用 VSTS 构建 IOS,运行命令后出现以下错误:cordova build ios 平台“android”似乎不是有效的 cordova 平台。它缺少 API.js。不支持安卓。 Cord
您使用什么软件/Wiki 来编写和分享有关开发人员、测试人员和管理人员的规范? 你使用维基系统,如果是,你使用什么维基软件? 或者您是否使用 Sharepoint 来管理和版本规范?将 SharePo
这是一家销售完整软件套件/平台的公司的示例 www.ql2.com/technology/platform.php 我想知道这样的套件/平台是如何开发的?你必须使用J2EE吗? 我更感兴趣的是这家公司
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有一个连接到套接字连接的应用程序,并且该连接向我发送了很多信息..可以说每秒 300 个订单(也许更多)..我有一个类(它就像一个监听器,对某个事件(并且该事件具有顺序)接收该顺序。创建一个对象,然
我即将开始一个 Netbeans 平台的项目。有没有人推荐他们用过并觉得有用的书籍或教程? 编辑: 这是一个已经开发好的swing应用。 最佳答案 除了 NetBeans 网站上的教程外,我还喜欢这本
有没有什么好的方法可以以非特定语言的方式定义接口(interface)/类层次结构,然后以特定语言生成相应的源代码?特别是,我需要同时针对 Java 和 C# 来创建一个相当全面的 API。我记得有一
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
大家晚上好我使用 API 平台,我想在创建实体时自动将所有者添加到我的实体中。我创建了一个事件来覆盖 API 平台,它获取当前用户并添加它。但是我的事件永远不会发生,但它确实存在于 debug:eve
这是一个有点奇怪的元编程问题,但我意识到我的新项目不需要完整的 MVC 框架,作为一个 Rails 人,我不确定现在该使用什么。 为您提供必要功能的要点;该网站将显示静态页面,但用户将能够登录并“编辑
这两天我的信息有点过载。 我打算建立自己的网站,允许本地企业列出他们的打折商品,然后用户可以进来搜索“Abercrombie T 恤”,然后就会列出出售它们的商店。 这是一个非常棒的小项目,我真的很兴
我的任务是为产品的下一代版本评估“企业”平台。我们目前正在考虑两种“类型”的平台——RAD(工作流引擎、集成 UI、工作流“技术插件”的小核心、状态的自动持久化……),例如 SalesForce.co
我需要一个不依赖于特定语言或构建系统的依赖管理器。我研究了几个优秀的工具(Gradle、Bazel、Hunter、Biicode、Conan 等),但没有一个能满足我的要求(见下文)。我还使用了 Gi
我在 Symfony 4 Flex 应用程序中使用 API Platform v2.2.5,该应用程序由一个功能 API 和 JWT Authentication 组成。 ,一些资源默认Open AP
虽然隐私法通常不属于我们开发人员的管辖范围,但我确实认为这是一个重要的话题,因为我们开发人员应该有责任警告我们的雇主,如果他们想要的东西会违反一些法律......在这种情况下,隐私法......通常情
我已经下载了 VisualVM 源代码,并尝试使用 Netbeans 7.01 编译 Glassfish 插件。这样做会导致以下错误: C:\source\visualvm\trunk\plugins
尝试 gradle 同步后...失败并在消息对话框中显示 Missing Android platform(s) detected: 'android-26' Install missing plat
大家好!我最近开始使用 Cordova,当我运行 Cordova platform add android 时,出现以下错误。我已经成功放置了 Java 和 Android SDK 的环境变量。但 n
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我是一名优秀的程序员,十分优秀!