微信公众平台接口文档错误高手进来

a573377769 2016-02-03 07:51:25
服务器配置成功,加载php微信示例代码出现,PHP Notice: Undefined index: echostr in D:\freehost\a573377769n\web\index.php on line 15
PHP Notice: Undefined index: signature in D:\freehost\a573377769n\web\index.php on line 70
PHP Notice: Undefined index: timestamp in D:\freehost\a573377769n\web\index.php on line 71
PHP Notice: Undefined index: nonce in D:\freehost\a573377769n\web\index.php on line 72
微信示例代码:
<?php
/**
* wechat php test
*/

//define your token
define("TOKEN", "ditu");
$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)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
libxml_disable_entity_loader(true);
$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()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}

$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}

?>
...全文
782 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
TR@SOE 2016-02-17
  • 打赏
  • 举报
回复
你不是用php test.php这样去运行搞的吧? 你需要在你的那个微信订阅号里发一个命令给你这个后台程序,这样才有post过来的内容。你直接在你机器的PHP环境下运行这个文件,当然找不到你要的POST数据了。 或者,另一种可能是你的PHP版本到了7以上,那么HTTP_RAW_POST_DATA已经不能用了。你必须回到5.6左右(我现在用的是5.5.9)。 最后,可以看下php.ini里面的一个设置,always_populate_raw_post_data,设置为1可能会有帮助。
jaksmine 2016-02-17
  • 打赏
  • 举报
回复
引用 3 楼 lovetxp 的回复:
总是出现“Notice: Undefined index:”这样的警告,这只是一个因为PHP版本不同而产生的警告(NOTICE或者WARNING),而非错误(ERROR)。 第一种方法:修改PHP配置文件,屏蔽掉此类警告和提示 修改php.ini配置文件,修改error_reporting为error_reporting = E_ALL & ~E_NOTICE 。这样程序中存在NOTICE和WARNING的不足之处的时候会被忽略,当然这对于新手来说并不合适,不仅不方便调试程序,而且不利于养成好的代码习惯。 第二种方法:对每个变量进行初始化 赋空值或者任意值即可,不影响运行。这对于变量比较多的程序来说比较繁琐,不过如果每次都提前设置下变量也是不错的习惯,例如:
正解
紫色田园 2016-02-15
  • 打赏
  • 举报
回复
总是出现“Notice: Undefined index:”这样的警告,这只是一个因为PHP版本不同而产生的警告(NOTICE或者WARNING),而非错误(ERROR)。 第一种方法:修改PHP配置文件,屏蔽掉此类警告和提示 修改php.ini配置文件,修改error_reporting为error_reporting = E_ALL & ~E_NOTICE 。这样程序中存在NOTICE和WARNING的不足之处的时候会被忽略,当然这对于新手来说并不合适,不仅不方便调试程序,而且不利于养成好的代码习惯。 第二种方法:对每个变量进行初始化 赋空值或者任意值即可,不影响运行。这对于变量比较多的程序来说比较繁琐,不过如果每次都提前设置下变量也是不错的习惯,例如:
a573377769 2016-02-06
  • 打赏
  • 举报
回复
php版本都尝试了还是不行
hyz20 2016-02-04
  • 打赏
  • 举报
回复
接口文档没问题,估计是PHP环境问题,用phpinfo看看PHP版本吧

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧