微信开发1之PHP成功获取微信支付的Token
生活随笔
收集整理的這篇文章主要介紹了
微信开发1之PHP成功获取微信支付的Token
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
相信不少同學在開發“微信支付”的時候遇到了無法獲取Token的難題,這里貼出我成功獲取Token的php源碼:
步驟如下:
1,確保你服務器的php環境是搭建好的,意思就是可以正確運行php程序
2,確定微信Token訪問你php程序的網址我的是"http://我的網站IP/Token3.php"
3,編寫Token3.php:
<?php header('Content-type:text'); define("TOKEN", "myname");//此處改成你在公眾號平臺上配置好的Token請求名稱,我用的是:“myname” $wechatObj = new wechatCallbackapiTest(); if (isset($_GET['echostr'])) {$wechatObj->valid(); }else{$wechatObj->responseMsg(); } class wechatCallbackapiTest {public function valid(){$echoStr = $_GET["echostr"];if($this->checkSignature()){header('content-type:text');echo $echoStr;exit;}}private function checkSignature(){$signature = $_GET["signature"];$timestamp = $_GET["timestamp"];$nonce = $_GET["nonce"];$token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);sort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}}public function responseMsg(){$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if (!empty($postStr)){$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($keyword == "?" || $keyword == "£?"){$msgType = "text";$contentStr = date("Y-m-d H:i:s",time());$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);echo $resultStr;}}else{echo "";exit;}} } ?>4,如有問題,請咨詢我的qq:1290851757,備注:csdn博客問問題的
如需更多資料或網課資源,請添加微信號:shuguangzhanzheng (涵蓋Java、web全棧、Ai深度學習、機器學習開發、前端、安卓等:慕課網,極客時間,開課吧,達內,老男孩,博學谷,咕泡學院,龍果學院,谷粒學院,馬士兵Java,尚硅谷,圣思園,貪心學院,萬門大學,圖靈學院,魯班學院,傳播智客,北風網,小碼哥,小象學院,51CTO,珠峰前端,拉鉤教育CSDN學院,麥子學院,蠻牛學院,陽明k8s,李振良k8s,百度前端學院等180TB網課資源)
總結
以上是生活随笔為你收集整理的微信开发1之PHP成功获取微信支付的Token的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 导入导出脚本_MySQL导入
- 下一篇: 广州海珠区计算机学校,2019广州海珠区