这是一个创建于 2433 天前的主题,其中的信息可能已经有所发展或是发生改变。
这两天正在看公众号开发的教程,为什么跟着教程走,上面的消息接口没反应呢?
用的是微信的示例接口文件啊。
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");//这里的 TOKEN,我已经在公众号后台设了
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract 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(!empty( $keyword )) //非空字符的回复,根本没有啊。
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
4 条回复 • 2018-03-27 16:45:07 +08:00
|
|
1
macwhirr 2018-03-27 15:31:12 +08:00
sort($tmpArr, SORT_STRING);
|
|
|
2
vrain 2018-03-27 16:33:30 +08:00
那一行,我代码上有,复制过来的时候,没粘上。 @ macwhirr
|
|
|
3
vrain 2018-03-27 16:34:55 +08:00
|
|
|
4
HanSonJ 2018-03-27 16:45:07 +08:00
|