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

我也写了个公众微信的

浏览:3690 发布日期:2013年11月19日 分类:系统代码 关键字: 微信
基于THINKPHP的公众微信开发,自己写的代码的,写的不好,轻喷
<?php

/**
* @author zhiwupei
*
*
*/
class WupeiAction extends Action
{
//微信、易信验证token
public $token='wupei';
//微信、易信提交过来的内容
public $postObj;
//用户回复的内容
public $keyword;
//用户关注事件
public $Event;
//点击菜单事件
public $evenkey;
//公众微信ID号
public $myid;
//用户ID号
public $userid;
//回复XML模板
public $xml;
//回复消息类型
public $msgType;
//微信提供的APPID
public $APPID;
//微信提供的APPSECRET
public $APPSECRET;
public $PicUrl;
public $MsgType;
public $Location_X;
public $Location_Y;
public $textTpl;
function _initialize(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->Event = $postObj->Event; //关注事件
$this->evenkey = $postObj->EventKey;//菜单事件
$this->myid = $postObj->ToUserName;
$this->userid = $postObj->FromUserName;
$this->PicUrl=$postObj->PicUrl;
$this->MsgType=$postObj->MsgType;
if($this->MsgType=="text"){
$this->keyword = trim($postObj->Content);
}else if($this->MsgType=="location"){
$this->Location_X=$postObj->Location_X;
$this->Location_Y=$postObj->Location_Y;
}
}
}
function index(){
//如果 是验证的时候,请把下面一行的注释去了
// $this->valid();
if($this->MsgType=="text"){
$this->sendMessage($this->keyword);
}else if($this->MsgType=="location"){

}
}
/**
* @param 回复消息的内容 $content
*
*/
function sendMessage($content){

$this->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>";
$this->msgType = "text";
$resultStr = sprintf($this->textTpl, $this->userid, $this->myid, time(), $this->msgType, $content);
echo $resultStr;
exit();

}
/**
* @param unknown $arr
* $arr包括title标题,intro简介,imagepath图片路径,url访问路径,四个属性
* 图片路径用绝对路径,即http://开头
*/
function sendNews($arr){
$this->msgType = "news";
$this->textTpl="<xml>
<ToUserName><![CDATA[".$this->userid."]]></ToUserName>
<FromUserName><![CDATA[".$this->myid."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[".$this->msgType."]]></MsgType>
<ArticleCount>".count($arr)."</ArticleCount>
<Articles>";

foreach ($arr as $key =>$list){
$this->textTpl.="<item>
<Title><![CDATA[".$list['title']."]]></Title>
<Description><![CDATA[".$list['name']."]]></Description>
<PicUrl><![CDATA[".$list['imagapath']."]]></PicUrl>
<Url><![CDATA[".$list['url']."]]></Url>
</item>
";
}
$this->textTpl.="</Articles></xml>";
echo $this->textTpl;
exit();
}

/**
*验证token方法
*/
public function valid(){
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}

function checkSignature(){
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$tmpArr = array($this->token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

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

function startmemu(){
header("Content-type: text/html; charset=utf-8");
$APPID = "wx01bee286793e0383";
$APPSECRET = "84e324cb2daec6a8a1aea8b19cce465d";
$ACCESS_TOKEN = "kgfind";
$data = ' {
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"type":"click",
"name":"歌手简介",
"key":"V1001_TODAY_SINGER"
},
{
"name":"菜单",
"sub_button":[
{
"type":"view",
"name":"搜索",
"url":"http://www.soso.com/"
},
{
"type":"view",
"name":"视频",
"url":"http://v.qq.com/"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET."");
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);
$token = curl_exec($ch);
if (curl_errno($ch)) {
echo 'CURL error on:'.curl_error($ch);
} else {
$arr = json_decode($token, true);
$ACCESS_TOKEN = $arr['access_token'];
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$ACCESS_TOKEN}");
$rs = curl_exec($ch);
if (curl_errno($ch)) {
echo 'CURL error on:'.curl_error($ch);
} else {
$arr = json_decode($rs, true);
echo "Code:" . $arr['errcode'];
echo "<br>Message:" . $arr['errmsg'];
}
}

curl_close($ch);
}
}
?>
评论() 相关
收藏
zhiwupei
积分:424 等级:LV2
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

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

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