用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > php教程

基于微信公众平台开发SDK(demo)扩展例子

时间:2015-01-23  来源:西部数据  作者:西部数据

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

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