20,359
社区成员
发帖
与我相关
我的任务
分享
<?php
include_once "Response.php";
define("CORPID","wx5aae8fe610db907c");
define("CORPSECRET","zmEbFeMuTwVJlUiZdv2-502ULoM0qJX3RUrXywMXnC_rqZgGuJIX3MUAnwWBQ6cQ");
$token_access_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".CORPID."&corpsecret=".CORPSECRET;
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
$res = file_get_contents($token_access_url);
$arr_result = json_decode($res,true);
define("ACCESS_TOKEN",$arr_result['access_token']);
$make_menu_url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?access_token=".ACCESS_TOKEN."&agentid=2";
$menudata = '{
"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, $make_menu_url);
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, $menudata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
$info = curl_exec($ch);
if(curl_errno($ch)){
echo 'Errno'.curl_error($ch);
}
curl_close($ch);
print_r($info);
?>
<?php
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"];
$sendmsg = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".ACCESS_TOKEN;
//extract post data
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$MsgType = $postObj->MsgType;
$MsgId = $postObj->MsgId;
$Event = $postObj->Event;
$EventKey = $postObj->EventKey;
$CreateTime = intval($postObj->CreateTime);
$formTime = date("Y-m-d H:i:s",$CreateTime);
$agentid = $postObj->AgentID;
$msg = "开发者id:".$toUsername."\n";
$msg .= "用户id:".$fromUsername."\n";
$msg .= "事件消息id:".$MsgId."\n";
$msg .= "事件消息类型id:".$MsgType."\n";
$msg .= "事件类型:".$Event."\n";
$msg .= "事件KEY值,与自定义菜单接口中KEY值对应:".$EventKey."\n";
$msg .= "事件发送过来的时间戳:".$CreateTime."\n";
$msg .= "消息发生具体时间:".$formTime."\n";
/* $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>"; */
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Event><![CDATA[%s]]></Event>
<EventKey><![CDATA[%s]]></EventKey>
<AgentID>%s</AgentID>
</xml>";
$contentStr = $msg;
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$formTime,$MsgType,$Event,$EventKey, $agentid);
echo $resultStr;
}
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;
}
}
}
?>