<?php
/**自己写的
*/
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
$wechatObj->responseMsg();//响应消息
$wechatObj->set_menu();//自定义菜单
class wechatCallbackapiTest
{
10 /**
11 * 绑定url、token信息
12 */
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
24 /**
25 * 接收消息,并自动发送响应信息
26 */
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
//提取post数据
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";
// 天气预报
if (strlen($keyword)==6) {
if ($keyword=='天气') {
$contentStr = "PengchongLee的商城欢迎你!\n温馨小提示:\n输入【天气+城市】可获取实时天气!\n例如:天气北京";
}else{
$contentStr = "PengchongLee的商城欢迎你!\n温馨小提示:\n输入【天气+城市】可获取实时天气!\n例如:天气北京";
}
}elseif (strlen($keyword)>=12) {
$city = substr($keyword,6);
$weather = $this->getWeather($city);
if ($weather['success']==0) {
$contentStr = "PengchongLee的商城欢迎你!\n温馨小提示:\n输入【天气+城市】可获取实时天气!\n例如:天气北京";
}elseif($weather['success']==1){
$contentStr = "实时天气:\n城市:【".$weather['result']['citynm']."】;\n日期:【".
$weather['result']['days']."】;\n星期:【".$weather['result']['week']."】;\n温度:【".
$weather['result']['temperature']."】;\n天气:【".$weather['result']['weather']."】;";
}
}else{
$contentStr = "PengchongLee的商城欢迎你!\n温馨小提示:\n输入【天气+城市】可获取实时天气!\n例如:天气北京";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
66 /**
67 * 检查签名,确保请求是从微信发过来的
68 */
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;
}
}
88 /**
89 * 自定义菜单
90 */
public function set_menu()
{
$access_token = $this->check_token();
if ($access_token=='no') {
}else{
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token";
$post_data = '
{
"button":[
{
"type":"view",
"name":"首页",
"url":"http://www.lpcblog.com/weixin/shop/"
},
{
"name":"个人中心",
"sub_button":[
{
"type":"view",
"name":"个人信息",
"url":"http://www.lpcblog.com/weixin/shop/user.html"
},
{
"type":"view",
"name":"个人账户",
"url":"http://www.lpcblog.com/weixin/shop/myuser.html"
}]
},
{
"type":"click",
"name":"关于我们",
"key":"V1001_TODAY_MUSIC"
}
]
}';
//设置菜单也是post传值
return json_decode($this->curl($url,$post_data);
}
}
//判断token值时效方法
public function check_token()
{
if (file_exists('token.txt'))
{
//判断token值时效,修改时间
$mtime=filemtime("token.txt");
if((time()-$mtime)<7000){
return $this->read_access_token();
}else{
$this->mem_token();
return $this->read_access_token();
}
}else{
$this->mem_token();
return $this->read_access_token();
}
}
//curl封装类
public function curl($url,$data=array())
{
// 初始化curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 开启支持https
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
// 关闭curl
curl_close($ch);
return $output;
}
//获取access_token方法
public function get_token()
{
//加载常量文件(这里是定义的申请的appid等常量)
include('define.php');
//微信获取access_token地址
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APP_SECRET;
//传值方式POST
$post_data = array(
'grant_type'=>'client_credential',
'appid'=>APPID,
'client_secret'=>APP_SECRET,
);
//curl方法模拟提交获取access_token(格式json)
$access_token = json_decode($this->curl($url,$post_data);
if($access_token['access_token']){
return $access_token;
}else{
return "获取access_token失败";
}
}
//读取access_token的方法
public function read_access_token()
{
$token = unserialize(file_get_contents('token.txt'));
return $token['access_token'];
}
//存token方法
public function mem_token()
{
//调用获取access_token的方法
$access_token = $this->get_token();
//序列化返回的access_token
$txt = serialize($access_token);
//保存access_token
file_put_contents('token.txt',$txt);
}
/**
* 获取天气
*/
public function getWeather($city='')
{
//加载常量文件
include('define.php');
//实时天气接口
$url = "http://api.k780.com/?app=weather.today&weaid=".$city."&appkey=".NOWAPI_APPKEY."&sign=".NOWAPI_SIGN."&format=json";
$post_data = '';
$weather = json_decode($this->curl($url),true);
return $weather;
}
}