栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

微信公众平台DEMO(PHP)

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信公众平台DEMO(PHP)

本人在SAE环境下搭建了CI框架(其实这个小东西用不着用框架的),直接把代码写在了控制器里面

responseMsg();
   
  }
   
  // chat response
  public function responseMsg()
  {
    //get post data, May be due to the different environments
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 
     //extract post data
    if (!empty($postStr)){

      $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
      $fromUsername = $postObj->FromUserName;
      $toUsername = $postObj->ToUserName;
      $MsgType = $postObj->MsgType;
      $time = time();

      switch($MsgType){
 case('text'):
   $keyword = trim($postObj->Content);
   $textTpl = "
  
  
  %s
  
  
  ";
   switch($keyword){
     case(1):
// Need to optimize
// random read data from jokes
$sql = 'SELECt * FROM jokes';
$query = $this->db->query($sql);
$res = $query->result_array();
$num_rows = $query->num_rows();
$key = rand(0, $num_rows - 1); // Notice: The value of key is from 0.
//$contentStr = $key.'#'.$res[$key ]['content']; // debug
$contentStr = $res[$key ]['content']; 
break;
     case(2):
$contentStr = 'Your fromUsername is: '.$fromUsername; 
break;      
     case(3):
$newsTpl = "
      
      
      %s
      
      2

      
 
   <![CDATA[%s]]> 
   
   
   
 
 
   <![CDATA[%s]]>
   
   
   
 
      

       ";

$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time,'news',
  '百度','', 'http://www.baidu.com/img/bdlogo.gif', 'http://www.baidu.com',
  'Google','', '', 'http://www.google.com'); // Notice: Google's logo is not suitable.
echo $resultStr;
exit; // Notice: It's exit, not break.  
     case(4):
$contentStr = "该功能正在开发中,敬请期待..."; 
break;
 
     
      
     default:
$contentStr = "回复数字 选择服务n";
$contentStr .= "1 笑话精选n";
$contentStr .= "2 获取您的Username...n";  
$contentStr .= "3 图文消息示例n"; 
$contentStr .= "4 开发中...n"; 
break;
   }
   $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
   break;
    
 case('image'):
    
   $media_id = $postObj->MediaId;
   $imgTpl = "
  
  
  %s
  
  
    
  
  ";
   $resultStr = sprintf($imgTpl, $fromUsername, $toUsername, $time, 'image', $media_id);
   break;
    
   // try get the id of the receive image and analyse
   
    
 case('voice'):
   $textTpl = "
  
  
  %s
  
  
  "; 
   $contentStr = '你说啥?俺听不见...';
   $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
   break;
  
 
  
 default:
   $resultStr = "Input something...";
   break;
      }
      echo $resultStr;

    }else {
      echo "";
      exit;
    }
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/43430.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号