基于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);
}
}
?>