微信公众平台DEMO(PHP)
来源: 阅读:1761 次 日期:2016-08-24 16:41:19
温馨提示: 小编为您整理了“微信公众平台DEMO(PHP)”,方便广大网友查阅!

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

//define your token

define("TOKEN", "109");

class Welcome extends CI_Controller {

public function index()

{

/*

$this->load->helper('url');

$this->load->view('welcome_message');

*/

// use chat response

$this->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;

/* others */

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

/*

$media_id = $postObj->MediaId;

$textTpl = "

%s

";

$length = strlen($media_id);

$contentStr = "I have received the image message you sent, the id of this image is # $media_id #, and the length of media_id is # $length #";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);

break;

*/

case('voice'):

$textTpl = "

%s

";

$contentStr = '你说啥?俺听不见...';

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);

break;

/* others */

default:

$resultStr = "Input something...";

break;

}

echo $resultStr;

}else {

echo "";

exit;

}

}

}

更多信息请查看 网络编程
手机网站地址: 微信公众平台DEMO(PHP)
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2026上岸·考公考编培训报班

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们| 联系我们| 人才招聘| 网站声明| 网站帮助| 非正式的简要咨询| 简要咨询须知| 新媒体/短视频平台| 手机站点| 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65099533/13759567129 获取招聘考试信息及咨询关注公众号:
咨询QQ:1093837350(9:00—18:00) 版权所有:
云南网警报警专用图标
Baidu
map