首页 > 代码库 > 微信公众号开发入门---STATE 1:入门回复,菜单

微信公众号开发入门---STATE 1:入门回复,菜单

public $fromUsername;
public $time;
public $toUsername;
public $postObj;
public $keyword;
public function actionTestwx(){
$echoStr = Yii::$app->request->get(‘echostr‘);
if ($this->checkSignature() && Yii::$app->request->get(‘echostr‘)) {
Yii::$app->response->content = $echoStr;
Yii::$app->response->send();
}else{
$this->responseMsg();
}
}
 
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = ‘lin886412‘;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
 
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function responseMsg()
{
$postStr = file_get_contents("php://input");
$this->postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
$this->fromUsername = $this->postObj->FromUserName[0];
$this->toUsername = $this->postObj->ToUserName;
$this->keyword = trim($this->postObj->Content);
$this->time = time();
if (strtolower($this->postObj->MsgType) == ‘event‘) {
if (in_array(strtolower($this->postObj->Event), [‘subscribe‘, ‘scan‘])) {
$this->msg_text("您已成功登陆木辰于平台。回复 ‘1’可测试智商");
//scan事件处理
}elseif (strtolower($this->postObj->Event) == ‘click‘){
//点击事件
switch ($this->postObj->EventKey) {
case "V1001_NEWCOMER":
$this->msg_text("1");
break;
default:
break;
}
}
} else {
if (strtolower($this->postObj->MsgType) == ‘text‘){
if ($this->keyword == ‘1‘){
$this->msg_text(‘傻逼‘);
}
}
}
}
public function msg_text($text)
{
$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>";
$msgType = "text";
$contentStr = $text;
$resultStr = sprintf($textTpl, $this->fromUsername, $this->toUsername, $this->time, $msgType, $contentStr);
echo $resultStr;
}
 
public function actionAddmenu(){
$appid = ‘wx78a3c689a8dbccc6‘;
$appsecret = ‘0ef78d4ef4c7911e0a533d8ec8fc3940‘;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$data = http://www.mamicode.com/CurlsUtil::get($url);
$data = http://www.mamicode.com/json_decode($data);
$token = @$data->access_token;
$url=‘ https://api.weixin.qq.com/cgi-bin/menu/create?access_token=‘.$token;
$content=‘
{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"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"
}]
}]
}‘;
$result = $this->https_post($url,$content);
$menu = json_decode($result);
var_dump($menu->errcode);
}
private function https_post($url, $data = http://www.mamicode.com/null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
 
/**
* 获取微信的access_token 2小时有效,存session
* @return mixed
*/
public function getWxAccessToken()
{
$token = Yii::$app->redis->get(‘access_token‘);
if ($token){
return $token;
}
$wxin_conf = Yii::$app->params[‘oauth_conf‘][‘oauth_wx_in‘];
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$wxin_conf[‘app_id‘]."&secret=".$wxin_conf[‘app_key‘];
$data = http://www.mamicode.com/CurlsUtil::get($url);
$data = http://www.mamicode.com/json_decode($data);
$token = @$data->access_token;
if ($token == null){
$data = http://www.mamicode.com/CurlsUtil::get($url);
$data = http://www.mamicode.com/json_decode($data);
$token = @$data->access_token;
}
Yii::$app->redis->setex(‘access_token‘,3600,$token);
return $token;
}

微信公众号开发入门---STATE 1:入门回复,菜单