搜索
系统检测到您的用户名不符合规范:

怎么把微信公众平台与think结合起来

浏览:1559 发布日期:2014年02月19日 分类:求助交流 关键字: 微信公众平台
我用原生态的php代码可以实现自定义菜单,自动回复等。
把原生态的类移植到think的扩展类之后在indexAction导入import('ORG.Util.Weixin');

define("ACCESS_TOKEN", 'WzgxWP9gkY7hvzZI8LItWgE62ZHMvzJKc_8iGZ8jbjR74kx9tBJ9A9IrPDhRJ9GbySL-zD-4iLmnAkyA6ULsAHLQ0qWwfGEFDIpY5E1fys9FGbUbiTjLnSDEqjluPnpr-vXpiXweq8nN-VBR_uViiA');
$wechatObj = new WechatCallbackapiTest();
$wechatObj->responseMsg();
然后在通过微信互动就没有反映了,不知道那里出问题了求大神解决

类文件 weixin.class.phpclass WechatCallbackapiTest{
public function valid() {
$echoStr = $_GET["echostr"];

//valid signature , option
if ($this->checkSignature()) {
echo $echoStr;
exit;
}
}

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;
$keyword = trim($postObj->Content);
$Event = trim($postObj->Event);
$EventKey = trim($postObj->EventKey);
$time = time();

//文本模版
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
//测试事件推送
if($Event=="subscribe"){
$contentStr = "欢迎访问思微创想网络工作室微信公众平台!";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
if($EventKey=="V1001_TODAY_MUSIC"){
$contentStr = "听歌!";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
if($EventKey=="V1001_TODAY_SINGER"){
$contentStr = "歌手简介!";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
if($EventKey=="V1001_GOOD"){
$contentStr = "赞一个!";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
if($EventKey=="V1001_HAO"){
$contentStr = "好!";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
//test
if($keyword==1){//文本
$contentStr = $keyword . "测试发送文本!";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else if($keyword==2){//音乐
$textTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>
<FuncFlag>0</FuncFlag>
</xml>";

$msgType = "music";
$msgtitle="致青春";
$Description="电影《致我们终将逝去的青春》 主题曲";
$MusicUrl="http://music.baidu.com/song/40153340#";
$HQMusicUrl="http://music.baidu.com/song/40153340#";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $msgtitle,$Description,$MusicUrl,$HQMusicUrl);
echo $resultStr;
}else if($keyword==3){//图文
$textTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml> ";

$msgType = "news";
$title1="致青春";
$Description="电影《致我们终将逝去的青春》 主题曲";
$PicUrl="http://img10.360buyimg.com/da/g13/M05/01/02/rBEhU1G6iA8IAAAAAADmPyFSPhUAAANiwAxLNcAAOZX575.jpg";
$Url="http://sale.jd.com/act/qYHkylDS0LVOg.html?sid=2&cid=601&aid=3678&bid=661&unit=36617&advid=60724&guv=";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $title1,$Description,$PicUrl,$Url);
echo $resultStr;
}
//
if (!empty ($keyword)) {

$contentStr = $keyword . "Welcome to wechat world!";

$msgType = "text";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
} else {
echo "Input something...";
}

} else {
echo "";
exit;
}
}

private function checkSignature() {
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

$token = TOKEN;
$tmpArr = array (
$token,
$timestamp,
$nonce
);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);

if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}

public function createMenu($data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".ACCESS_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);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}

}
最佳答案
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
漠皓
积分:573 等级:LV2
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /