| 
 
TA的每日心情|  | 慵懒 2018-3-14 09:47
 | 
|---|
 签到天数: 16 天 [LV.4]偶尔看看III 注册会员 
 
 
	积分159 
 | 
 
| 没别的分享个工作心得,有问题欢迎咨询。 <?php
 /**
 * 华全柏柏微信验证TOKEN绑定。
 */
 
 //define your token
 define("TOKEN", "2935370owf");
 $wechatObj = new wechatCallbackapiTest();
 //$wechatObj->valid();
 $wechatObj->responseMsg();
 
 class wechatCallbackapiTest
 {
 //事物处理函数
 public function responseMsg()
 {
 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 if (!empty($postStr)){
 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>";
 switch($keyword){
 case "马云":
 $msgType = "text";
 $contentStr="您好,马云!我知道您创建了阿里巴巴!";
 break;
 default :
 $msgType = "text";
 $contentStr="您好,马云!我知道您创建了阿里巴巴!";
 break;
 }
 if(!empty($contentStr)){
 //执行
 echo set_msg();
 //输出到微信终端
 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
 echo $resultStr;
 }
 }else {
 echo "";
 exit;
 }
 }
 
 //接口验证函数
 public function valid()
 {
 $echoStr = $_GET["echostr"];
 
 //valid signature , option
 if($this->checkSignature()){
 echo $echoStr;
 //exit;
 }
 }
 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 );
 
 if( $tmpStr == $signature ){
 return true;
 }else{
 return false;
 }
 }
 }
 
 ?>
 
 
 <?php
 //获取微信access_token
 function getaccess_token(){
 //appid与appsecret改成你自己的
 $appid = 'wx84275d9720a105ea';
 $appsecret = '24aec43427f59cf916493aff628f396f';
 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
 $data = curl_exec($ch);
 curl_close($ch);
 $data = json_decode($data,true);
 return $data['access_token'];
 }
 //设置与发送模板信息
 function set_msg(){
 //获取access_token
 $access_token = getaccess_token();
 //这里是在模板里修改相应的变量
 $formwork = '{
 "touser":"onWFtxPXkzAt-M01jiXDjZOnVDYk",
 "template_id":"e6wr4qcZJOL8js1IGBl9edoyvPBdvE3VQR11RWnoMKc",
 "url":"http://www.wangwenxiao.com",
 "data":{
 "title": {
 "value":"这里是自己定义的标题",
 "color":"#173177"
 },
 "content":{
 "value":"这里是自定义内容,点击进入王文晓博客,啦啦啦",
 "color":"#173177"
 },
 "time": {
 "value":"这里填写时间",
 "color":"#173177"
 }
 }
 }';
 $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
 curl_setopt($ch, CURLOPT_POST,1);
 curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
 $data = curl_exec($ch);
 curl_close($ch);
 return $data;
 }
 
 ?>
 
 
 | 
 |