当前位置:首页 > PHP教程 > php高级应用 > 列表

php实现基于微信公众平台开发SDK(demo)扩展的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-04 15:27:12 浏览: 评论:0 

这篇文章主要介绍了php实现基于微信公众平台开发SDK(demo)扩展的方法,包括处理文本消息、天气、翻译、聊天信息及自定义菜单等,需要的朋友可以参考下

本文实例讲述了php实现基于微信公众平台开发SDK(demo)扩展的方法。分享给大家供大家参考。具体分析如下:

该扩展基于官方的微信公众平台SDK,这里只做了简单地封装,实现了一些基本的功能(如天气查询,翻译,自动聊天机器人,自定义菜单接口)仅供学习之用.代码如下:

  1. define("TOKEN""xingans"); 
  2. $wechatObj = new wechatCallbackapiTest(); 
  3. $wechatObj->responseMsg(); 
  4. class wechatCallbackapiTest 
  5.    //签名验证公共接口 
  6.    public function valid() 
  7.    { 
  8.        $echoStr = $_GET["echostr"]; 
  9.        if($this->checkSignature()){ 
  10.            echo $echoStr
  11.            exit
  12.        } 
  13.    } 
  14.  
  15.    //主入口处理函数 
  16.    public function responseMsg() 
  17.    { 
  18.        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
  19.        if (!emptyempty($postStr)){ 
  20.            $postObj = simplexml_load_string($postStr'SimpleXMLElement', LIBXML_NOCDATA); 
  21.            $msgType=trim($postObj->MsgType); 
  22.            
  23.            switch($msgType){ 
  24.                case 'text'
  25.                    $resultStr=$this->handleText($postObj); 
  26.                    break
  27.                case 'event'
  28.                    $resultStr=$this->handleEvent($postObj); 
  29.                    break
  30.                default
  31.                    $resultStr=$this->handleDefault($postObj); 
  32.                    break
  33.            } 
  34.            echo $resultStr
  35.            
  36.        }else { 
  37.            echo "Error"
  38.            exit
  39.        } 
  40.    } 
  41.    
  42.    //处理文本消息 
  43.       private function handleText($obj){ 
  44.           $keyword=trim($obj->Content); 
  45.           if(preg_match('/天气/',$keyword)){ 
  46.               $contentStr=$this->handleWeather($obj); 
  47.           }elseif(preg_match('/翻译/',$keyword)){ 
  48.               $contentStr=$this->handleTranslation($obj); 
  49.           }else
  50.               $contentStr=$this->handleChat($obj); 
  51.           } 
  52.           return $this->handleStr($obj,$contentStr); 
  53.       } 
  54.       
  55.       //处理天气 
  56.       private function handleWeather($obj){ 
  57.           $keyword=mb_substr($obj->Content,-2,2,'utf-8'); 
  58.           $zone=mb_substr($obj->Content,0,-2,'utf-8'); 
  59.           if($keyword=='天气' && !emptyempty($zone)){ 
  60.               $zoneArr=json_decode(file_get_contents('http://api.k780.com:88/?app=weather.city&format=json'),true); 
  61.               $zoneArr=$zoneArr['result']; 
  62.               $cityId=''
  63.               foreach($zoneArr as $value){ 
  64.                   if($zone==$value['citynm']){ 
  65.                       $cityId=$value['weaid']; 
  66.                       break
  67.                   } 
  68.               } 
  69.               
  70.               if(!emptyempty($cityId)){ 
  71.                   $data=file_get_contents("http://api.k780.com:88/?app=weather.today&weaid=$cityId&appkey=10638&sign=3736578f099375665f9f141a6326b757&format=json"); 
  72.                   $data=json_decode($data); 
  73.                   $contentStr="今天是:".$data->result->days.",".$data->result-> week.",".$data->result->citynm."天气:".$data->result->weather."n温度:".$data->result->temperature.", 
  74.                   ".$data->result->wind_direction.",".$data->result->wind_power.", 最低温度:".$data->result->temp_low.",最高温度:".$data->result->temp_high; 
  75.               }else
  76.                   $contentStr='找不到输入的城市!'
  77.               }    
  78.           }else
  79.               $contentStr='输入的查询格式不正确!'
  80.           } 
  81.           return $contentStr
  82.       } 
  83.       
  84.       //处理翻译 
  85.       private function handleTranslation($obj){ 
  86.        $keyword=mb_substr($obj->Content,0,2,'utf-8'); 
  87.        $words=mb_substr($obj->Content,2,220,'utf-8'); 
  88.        if($keyword=='翻译' && !emptyempty($words)){ 
  89.            $data=file_get_contents('http://fanyi.youdao.com/openapi.do?keyfrom=zfsblog&key=364295447&type=data&doctype=json&version=1.1&q='.urlencode($words));        
  90.            $data=json_decode($data,true); 
  91.            
  92.            switch($data['errorCode']){ 
  93.                case '0'
  94.                    $contentStr=$data['translation'][0]; 
  95.                    break
  96.                case '20'
  97.                    $contentStr='要翻译的文本过长'
  98.                    break
  99.                case '30'
  100.                    $contentStr='无法进行有效的翻译'
  101.                    break
  102.                case '40'
  103.                    $contentStr='不支持的语言类型'
  104.                    break
  105.                case '50'
  106.                    $contentStr='无效的key'
  107.                    break
  108.                default
  109.                    $contentStr='Error'
  110.                    break
  111.            } 
  112.        }else
  113.            $contentStr='输入的翻译格式不正确!'
  114.        } 
  115.        return $contentStr
  116.       } 
  117.       
  118.       //处理聊天信息 
  119.       private function handleChat($obj){ 
  120.        $keywords=$obj->Content; 
  121.        $curlPost=array("chat"=>$keywords); 
  122.        $ch = curl_init();//初始化curl 
  123.        curl_setopt($ch, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');//抓取指定网页 
  124.        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header 
  125.        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 
  126.        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 
  127.        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
  128.        $data = curl_exec($ch);//运行curl 
  129.        curl_close($ch); 
  130.        
  131.        if(!emptyempty($data)){ 
  132.            $contentStr=$data
  133.        }else
  134.            $ran=rand(1,5); 
  135.            
  136.            switch($ran){ 
  137.                case 1: 
  138.                    $contentStr"小九今天累了,明天再陪你聊天吧"
  139.                    break
  140.                case 2: 
  141.                    $contentStr"小九睡觉喽~~"
  142.                    break
  143.                case 3: 
  144.                    $contentStr"呼呼~~呼呼~~"
  145.                    break
  146.                case 4: 
  147.                    $contentStr"你话好多啊,不跟你聊了"
  148.                    break
  149.                case 5: 
  150.                    $contentStr"你话好多啊,不跟你聊了"
  151.                    break
  152.            } 
  153.        } 
  154.        return $contentStr
  155.       } 
  156.       
  157.       //创建自定义菜单 
  158.       public function createMenu($data,$token){ 
  159.         $ch = curl_init(); 
  160.         curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$token); 
  161.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
  162.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  163.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
  164.         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); 
  165.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  166.         curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
  167.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
  168.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  169.         $result = curl_exec($ch);      
  170.         curl_close($ch); 
  171.         return $result
  172.       } 
  173.       
  174.       //查询自定义菜单 
  175.    function getMenu($token){ 
  176.        $url="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=$token"
  177.        $ch = curl_init($url); 
  178.        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true) ; //获取数据返回 
  179.        curl_setopt($ch, CURLOPT_BINARYTRANSFER,true) ; //在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
  180.        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); 
  181.        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false); 
  182.        return $output = curl_exec($ch);        
  183.       } 
  184.       
  185.       //删除自定义菜单 
  186.       public function deleteMenu($token){ 
  187.        $url="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$token
  188.        $ch = curl_init(); 
  189.        curl_setopt($ch, CURLOPT_URL, $url); 
  190.        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  191.        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  192.        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
  193.        $result = curl_exec($ch); 
  194.        curl_close($ch); 
  195.        return $result
  196.       } 
  197.       //处理事件消息 
  198.       private function handleEvent($obj){ 
  199.           $content=''
  200.           switch($obj->Event){ 
  201.               case 'subscribe'
  202.                   $content.="welcome-欢迎关注该公众号号!"
  203.                   break
  204.               case 'unsubscribe'
  205.                   $content.="感谢您一直以来对该公众号的关注,再见!"
  206.                   break
  207.               default
  208.                   $content.=""
  209.                   break
  210.           } 
  211.           return $this->handleStr($obj,$content); 
  212.       } 
  213.       
  214.       //处理回复消息字符串 
  215.       private function handleStr($obj,$content='',$flag=0){ 
  216.        $textTpl = ""
  217.        return sprintf($textTpl$obj->FromUserName, $obj->ToUserName, time(), $content,$flag); 
  218.       } 
  219.    
  220.    //签名验证函数 
  221.    private function checkSignature() 
  222.    { 
  223.        $signature = $_GET["signature"]; 
  224.        $timestamp = $_GET["timestamp"]; 
  225.        $nonce = $_GET["nonce"];    
  226.        $token = TOKEN; 
  227.        $tmpArr = array($token$timestamp$nonce); 
  228.        sort($tmpArr, SORT_STRING); 
  229.        $tmpStr = implode( $tmpArr ); 
  230.        $tmpStr = sha1( $tmpStr ); 
  231.        if$tmpStr == $signature ){ 
  232.            return true; 
  233.        }else
  234.            return false; 
  235.        } 
  236.    } 

希望本文所述对大家的php程序设计有所帮助。

Tags: php微信公众平台 SDK demo

分享到: