服务器配置成功,加载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;
}
}
}?>
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;
}
}
}?>
第一种方法:修改PHP配置文件,屏蔽掉此类警告和提示
修改php.ini配置文件,修改error_reporting为error_reporting = E_ALL & ~E_NOTICE 。这样程序中存在NOTICE和WARNING的不足之处的时候会被忽略,当然这对于新手来说并不合适,不仅不方便调试程序,而且不利于养成好的代码习惯。
第二种方法:对每个变量进行初始化
赋空值或者任意值即可,不影响运行。这对于变量比较多的程序来说比较繁琐,不过如果每次都提前设置下变量也是不错的习惯,例如: