1. 程式人生 > >[php]微信測試號調取acces_token,自定義菜單以及被動響應消息

[php]微信測試號調取acces_token,自定義菜單以及被動響應消息

個人中心 res keyword environ 變量 pri exist ron 數據

  1 <?php
  2 /**自己寫的
  */ 3 $wechatObj = new wechatCallbackapiTest(); 4 $wechatObj->valid(); 5 $wechatObj->responseMsg();//響應消息 6 $wechatObj->set_menu();//自定義菜單 7 8 class wechatCallbackapiTest 9 { 10 /** 11 * 綁定url、token信息 12 */ 13 public function valid() 14 {
15 $echoStr = $_GET["echostr"]; 16 17 //valid signature , option 18 if($this->checkSignature()){ 19 echo $echoStr; 20 exit; 21 } 22 } 23 24 /** 25 * 接收消息,並自動發送響應信息 26 */ 27 public function responseMsg() 28 { 29 //
get post data, May be due to the different environments 30 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 31 32 //extract post data 33 //提取post數據 34 if (!empty($postStr)){ 35 36 $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
37 $fromUsername = $postObj->FromUserName;//發送人 38 $toUsername = $postObj->ToUserName;//接收人 39 $keyword = trim($postObj->Content);//消息內容 40 $time = time();//當前時間做為回復時間 41 42 $textTpl = "<xml> 43 <ToUserName><![CDATA[%s]]></ToUserName> 44 <FromUserName><![CDATA[%s]]></FromUserName> 45 <CreateTime>%s</CreateTime> 46 <MsgType><![CDATA[%s]]></MsgType> 47 <Content><![CDATA[%s]]></Content> 48 <FuncFlag>0</FuncFlag> 49 </xml>"; 50 if(!empty( $keyword )) 51 { 52 $msgType = "text"; 53 $contentStr = "lpc"; 54 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 55 echo $resultStr; 56 }else{ 57 echo "Input something..."; 58 } 59 60 }else { 61 echo ""; 62 exit; 63 } 64 } 65 66 /** 67 * 檢查簽名,確保請求是從微信發過來的 68 */ 69 private function checkSignature() 70 { 71 $signature = $_GET["signature"]; 72 $timestamp = $_GET["timestamp"]; 73 $nonce = $_GET["nonce"]; 74 75 $token = TOKEN; 76 $tmpArr = array($token, $timestamp, $nonce); 77 sort($tmpArr); 78 $tmpStr = implode( $tmpArr ); 79 $tmpStr = sha1( $tmpStr ); 80 81 if( $tmpStr == $signature ){ 82 return true; 83 }else{ 84 return false; 85 } 86 } 87 88 /** 89 * 自定義菜單 90 */ 91 public function set_menu() 92 { 93 $access_token = $this->check_token(); 94 95 if ($access_token==‘no‘) { 96 97 }else{ 98 $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token"; 99 100 $post_data = ‘ 101 { 102 "button":[ 103 { 104 "type":"view", 105 "name":"首頁", 106 "url":"http://www.lpcblog.com/weixin/shop/" 107 }, 108 { 109 "name":"個人中心", 110 "sub_button":[ 111 { 112 "type":"view", 113 "name":"個人信息", 114 "url":"http://www.lpcblog.com/weixin/shop/user.html" 115 }, 116 { 117 "type":"view", 118 "name":"個人賬戶", 119 "url":"http://www.lpcblog.com/weixin/shop/myuser.html" 120 }] 121 }, 122 { 123 "type":"click", 124 "name":"關於我們", 125 "key":"V1001_TODAY_MUSIC" 126 } 127 ] 128 }‘; 129 130 //設置菜單也是post傳值 131 return json_decode($this->curl($url,$post_data); 132 } 133 } 134 135 //判斷token值時效方法 136 public function check_token() 137 { 138 if (file_exists(‘token.txt‘)) 139 { 140 //判斷token值時效,修改時間 141 $mtime=filemtime("token.txt"); 142 if((time()-$mtime)<7000){ 143 return $this->read_access_token(); 144 }else{ 145 return ‘NO‘; 146 } 147 }else{ 148 $this->mem_token(); 149 return $this->read_access_token(); 150 } 151 } 152 153 //curl封裝類 154 public function curl($url,$data=array()) 155 { 156 // 初始化curl 157 $ch = curl_init(); 158 159 curl_setopt($ch, CURLOPT_URL, $url); 160 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 161 162 // 開啟支持https 163 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 164 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 165 // post數據 166 curl_setopt($ch, CURLOPT_POST, 1); 167 // post的變量 168 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 169 170 $output = curl_exec($ch); 171 // 關閉curl 172 curl_close($ch); 173 return $output; 174 } 175 176 //獲取access_token方法 177 public function get_token() 178 { 179 //加載常量文件 180 include(‘define.php‘); 181 //微信獲取access_token地址 182 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APP_SECRET; 183 184 //傳值方式POST 185 $post_data = array( 186 ‘grant_type‘=>‘client_credential‘, 187 ‘appid‘=>APPID, 188 ‘client_secret‘=>APP_SECRET, 189 ); 190 //curl方法模擬提交獲取access_token(格式json) 191 $access_token = json_decode($this->curl($url,$post_data); 192 if($access_token[‘access_token‘]){ 193 return $access_token; 194 }else{ 195 return "獲取access_token失敗"; 196 } 197 } 198 199 //讀取access_token的方法 200 public function read_access_token() 201 { 202 $token = unserialize(file_get_contents(‘token.txt‘)); 203 return $token[‘access_token‘]; 204 } 205 206 //存token方法 207 public function mem_token() 208 { 209 //調用獲取access_token的方法 210 $access_token = $this->get_token(); 211 //序列化返回的access_token 212 $txt = serialize($access_token); 213 //保存access_token 214 file_put_contents(‘token.txt‘,$txt); 215 } 216 }


[php]微信測試號調取acces_token,自定義菜單以及被動響應消息