//配置
第一步:
在微信公众平台基本配置里配置:
url: http://www.tjgbank.cn/wx_sample.php//wx_sample.php为上传到服务器上的demo
token:上传到服务器上的demo里面最上面的token;
:随机生成
:明文方式;
提交之后验证成功说明成功。
第二步:
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "tianjingang");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
const appID='wx5256b55ba9b6bd5f';
const appsecret='4546ba78ef39b6f05b6a9adff3d2a47c';
const access_token='BUUbDpHDMLy8mQ8gpWcGGZjWehgbTeJ5JxZdCVJYg565aI09oy4KJGtzcWZIGggmB9t7qKFEY5rTQwgcE6FwSMJoGGAIorMnzvrkUp5-3O1V2GEjUs80q5W5XrEWH1TnJSBdACANWF';
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature() && $echoStr){
echo $echoStr;
exit;
}else{
$this->responseMsg();//自动回复
echo $this->setmenu();//创建自定义菜单
}
}
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)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
libxml_disable_entity_loader(true);
$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($keyword==trim('您好')){
$contentStr="欢迎来到田金刚的订阅号";
}else{
$contentStr="客服人员田金刚为你服务";
}
//$contentStr=$this->getass_token();
//$contentStr = $this->contRand($keyword);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
//curl模拟表单提交
protected function setcurl($url,$data='',$method){
$ch = curl_init(); //1.初始化
curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
//4.参数如下
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
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_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
if($method=="POST"){//5.post方式的时候添加数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);//6.执行
if (curl_errno($ch)) {//7.如果出错
return curl_error($ch);
}
curl_close($ch);//8.关闭
return $tmpInfo;
}
//获取token
public function getass_token(){
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.self::appID.'&secret='.self::appsecret;
//return $url;
$arr=json_decode($this->setcurl($url,'POST'),true);
return $arr['access_token'];
}
//创建自定义菜单
public function setmenu(){
$url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.self::access_token;
$data=' {
"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"
}]
}]
}';
echo $this->setcurl($url,$data,'POST');//输出自定义菜单
}
private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
return true;//取消安全机制验证
/*if( $tmpStr == $signature ){
return true;
}else{
return false;
}*/
}
}
?>