相信不少同学在开发“微信支付”的时候遇到了无法获取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博客问问题的