国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java - PHP開發(fā)微信無(wú)法獲取到signature,timestamp,nonce

瀏覽:150日期:2022-09-25 14:42:14

問題描述

<?php namespace HomeController;use ThinkController;define('TOKEN','weixin');/* *微信的入口文件 */class WechatController extends Controller {

protected $User; //微信用戶對(duì)象 protected $app_id; protected $secret;/*通用入口 構(gòu)造方法 *aunthor:caodi *date:2015-09-25 */public function _initialize() { $this->app_id = C('APPID'); $this->secret = C('APPSECRET');}/*微信入口 *author:caodi *date:2015-09-22 */public function wechat() { DLOG('微信入口記錄的時(shí)間','run','caodi'); if ($_GET[’echostr’] != NULL ) { echo $_GET[’echostr’];exit; } //微信只會(huì)在第一次在URL中帶echostr參數(shù),以后就不會(huì)帶這個(gè)參數(shù)了 if ($this->checkSignature()) { //success!$postStr = $GLOBALS['HTTP_RAW_POST_DATA'];//extract post dataif (!empty($postStr)) { libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA); $this->$User = $postObj; //根據(jù)消息類型將信息分發(fā) $this->route($postObj); //exit; //以下為測(cè)試用的 $toUsername = $postObj->ToUserName; $fromUsername = $postObj->FromUserName; $keyword = trim($postObj->Content); $msyType = trim($postObj->MsgType); //消息類型 $event = trim($postObj->Event); //事件類型 $time = time(); $result = json_encode($postObj); DLOG('消息的參數(shù)'.$result,'run','caodi'); $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 ($event == 'subscribe') {$msgType = 'text';$contentStr = date('Y-m-d H:i:s',time());$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);echo $resultStr; }} } else {echo 'error'; }}/*wechat身份驗(yàn)證 *author:caodi *date:2015-09-22 */public function checkSignature() { //you must define TOKEN by yourselfif (!defined('TOKEN')) {throw new Exception('TOKEN is not defined!'); } $nonce = $_GET['nonce']; $token = TOKEN; $timestamp = $_GET['timestamp']; $signature = $_GET['signature']; echo $signature.'<br/>'; echo $timestamp.'<br/>'; echo $nonce.'<br/>';$tmpArr = array($token,$timestamp,$nonce); sort($tmpArr,SORT_STRING); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if ($tmpStr == $signature) {return true;echo 'true'; } else {return false;echo 'false'; }}/*根據(jù)微信的消息類型來(lái)進(jìn)行的分發(fā) *author:caodi *date:2015-09-23 */public function route($postObj) { $msgType = trim($postObj->MsgType); DLOG('mygtype='.$msgType,'run','caodi'); switch ($msgType) {//(1)接受的為消息推送case 'text': $this->reponse_text($postObj); break;case 'image': $this->reponse_image($postObj); break;case 'voice': $this->reponse_voice($postObj); break;//(2)接受的為事件推送case 'event': $event = $postObj->Event; DLOG('event='.$event,'run','caodi'); switch ($event) {case 'subscribe': $this->subscribe($postObj); break;case 'unsubscribe': $this->unsubscribe($postObj); break;//自定義菜單的事件功能 } }}/*微信用戶關(guān)注微信號(hào)事件(獲取用戶的基本信息存入到用戶表中去) *author:caodi *date:2015-09-23 */public function subscribe($postObj) { $open_id = $postObj->FromUserName; $create_time = $postObj->CreateTime; $UserDao = M('user'); //(1)根據(jù)用戶的open_id去 https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN $access_token = 'RQ4fmRD-a2JflW7_9-mmefNkHnK35aoZHHXn9PoB_vqDfxVWdT8XNbtfv5F1v1yK_b81Xar3So4gRLdlX6QxJfa5fGApcOAeLI_Fx3h9hxGjkNhUgADXidNBKIi5EjanHOZjADAVCN'; $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$open_id.'&lang=zh_CN'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求保存的結(jié)果到字符串還是輸出在屏幕上,非0表示保存到字符串中 curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //對(duì)認(rèn)證來(lái)源的檢查,0表示阻止對(duì)證書的合法性檢查 $result = curl_exec($ch); DLOG('result'.$result,'run','caodi'); curl_close($ch); $user_info = json_decode($result,true); //(2)將得到的用戶信息保存到數(shù)據(jù)庫(kù)中去 $data = array(); $data[’user_nick’] = $user_info[’nickname’]; $user_info[’sex’] = $user_info[’sex’] == 0 ? 1 : $user_info[’sex’]; //將性別為0的轉(zhuǎn)化為默認(rèn)的男性 $data[’user_sex’] = $user_info[’sex’]; $data[’user_avatar’] = $user_info[’headimgurl’]; $data[’user_type’] = 1;//用戶類型 1-普通用戶 2-助理 $open_id = json_decode($open_id,true); $data[’wx_open_id’] = $user_info[’openid’]; $data[’user_app_version’] = 'wechat9.0'; $data[’user_platform’] = 'wechat'; //當(dāng)前使用的設(shè)備平臺(tái) $data[’user_create_time’] = date('Y-m-d H:i:s',time()); $result = $UserDao->add($data); DLOG('sql= '.$UserDao->getlastsql(),'run','caodi'); if($result === false) {DLOG('數(shù)據(jù)庫(kù)插入失敗','run','caodi');exit; }}/*自定義菜單的生成 *author:caodi *date:2015-09-24 */public function create_menu(){ include_once(APP_PATH.'Common/Conf/menu_config.php'); $data = $menu_config; $access_token = 'RQ4fmRD-a2JflW7_9-mmefNkHnK35aoZHHXn9PoB_vqDfxVWdT8XNbtfv5F1v1yK_b81Xar3So4gRLdlX6QxJfa5fGApcOAeLI_Fx3h9hxGjkNhUgADXidNBKIi5EjanHOZjADAVCN'; $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); var_dump($result); exit;}/*通過OAuth2.0的網(wǎng)頁(yè)授權(quán)(自定義菜單中,獲取用戶的openID同時(shí)進(jìn)入我的任務(wù)頁(yè)) *author:caodi *date:2015-09-24 */public function my_task () { $code = $_GET[’code’]; $oprn_id = $this->code_to_openID($code); var_dump($code); echo 'caodi'.'<br>'; echo '<center><h1>{$open_id}</h1></center>';}/*由OAuth2.0獲取到的code轉(zhuǎn)化成用戶的openID *author:caodi *date:2015-09-24 */public function code_to_openID($code) { if (empty($code) == true) {DLOG('獲取的code為空','run','caodi');exit; } $appid = $this->app_id; $secret = $this->secret; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); DLOG('由OAuth2.0獲取到的code轉(zhuǎn)化成用戶的openID的結(jié)果='.$result,'run','caodi'); curl_close($ch); $user_info = json_decode($result,true); $open_id = $user_info[’openid’]; return $open_id;}

}?>

問題解答

回答1:

if ($_GET[’echostr’] != NULL ) {

echo $_GET[’echostr’]; exit;}對(duì)接的時(shí)候打印一下post和get。。不會(huì)沒有的

標(biāo)簽: 微信
相關(guān)文章:
主站蜘蛛池模板: aa级毛片毛片免费观看久 | 亚洲综合一区二区三区 | 免费看欧美毛片大片免费看 | 一级特黄特黄的大片免费 | 日韩欧美一区二区在线 | 日本三级欧美三级 | 朝鲜美女免费一级毛片 | 青青草福利视频 | 精品视频99 | 一级在线免费视频 | 欧美一区二区在线免费观看 | 一级做a爱片特黄在线观看 一级做a爱片特黄在线观看免费看 | 国产激情久久久久影 | 三级毛片在线播放 | 久久久久国产精品美女毛片 | 中文字幕视频网站 | 亚洲国产天堂久久综合图区 | 一级美国乱色毛片 | 草久在线观看视频 | 毛片免费在线观看 | 国产免费黄色网址 | 在线毛片免费 | 中文字幕日本不卡 | 亚州精品一区二区三区 | 在线成人播放毛片 | 日本高清视频免费在线观看 | 亚洲在线成人 | 亚洲高清成人欧美动作片 | 国产高中生粉嫩无套第一次 | 成人区精品一区二区毛片不卡 | 久久道 | 在线高清国产 | 秋霞手机入口二日韩区 | 欧美日本一区二区三区道 | 97国产免费全部免费观看 | 国产精品莉莉欧美自在线线 | 国产欧美日韩在线视频 | 亚洲加勒比| 国内精品视频成人一区二区 | 99久久精品费精品国产一区二区 | 久久精品国产第一区二区 |