<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
$appid="XXXX";//填写appid
$secret="XXXXX";//填写secret
$wechatObj->create_menu($appid,$secret);//创建菜单
class 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);
$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(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "123Welcome to the Macau International Airport Micro message platform, Micro message is being tested, the related query to Micro message platform account wechat1@aims.com.mo associated query!~!~";
$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, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function get_access_token($appid,$secret) {
//请求地址
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$ch = curl_init();//模拟地址请求地址
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$a = curl_exec($ch);//获取地址
curl_close($ch); //关闭
$strjson=json_decode($a);//json解析
$access_token = $strjson->access_token;//获取access_token
return $access_token;
}
//创建菜单
public function create_menu($appid,$secret){
$access_token = $this->get_access_token($appid,$secret);
//构建button
$post= '{
"button": [
{
"name": "航班信息",
"sub_button": [
{
"type": "view",
"name": "实事航班信息",
"url": "http://www.macau-airport.com"
},
{
"type": "view",
"name": "测试",
"url": "http://www.macau-airport.com"
},
{
"type": "view",
"name": "航班时刻表",
"url": "http://www.macau-airport.com"
}
]
},
{
"name": "机场服务",
"sub_button": [
{
"type": "view",
"name": "旅行预订服务",
"url": "http://www.macau-airport.com"
},
{
"type": "view",
"name": "豪华专车服务",
"url": "http://www.macau-airport.com"
},
{
"type": "view",
"name": "直通快线服务",
"url": "http://www.macau-airport.com"
}
]
},
{
"name": "机场设施",
"sub_button": [
{
"type": "view",
"name": "银行",
"url": "http://www.macau-airport.com"
},
{
"type": "view",
"name": "免税店",
"url": "http://www.macau-airport.com"
},
{
"type": "view",
"name": "百福小厨",
"url": "http://www.macau-airport.com"
},
{
"type": "view",
"name": "行李存放",
"url": "http://www.macau-airport.com"
},
{
"type": "view",
"name": "更多",
"url": "http://www.macau-airport.com"
}
]
}
]}'; //提交内容
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}"; //查询地址
$ch = curl_init();//新建curl
curl_setopt($ch, CURLOPT_URL, $url);//url
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, 1); //post
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//post内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch); //输出
curl_close($ch); //关闭
}
}
?>