服务器配置成功,加载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;
}
}
}?>

解决方案 »

  1.   

    接口文档没问题,估计是PHP环境问题,用phpinfo看看PHP版本吧
      

  2.   

    php版本都尝试了还是不行
      

  3.   

    总是出现“Notice: Undefined index:”这样的警告,这只是一个因为PHP版本不同而产生的警告(NOTICE或者WARNING),而非错误(ERROR)。
    第一种方法:修改PHP配置文件,屏蔽掉此类警告和提示
    修改php.ini配置文件,修改error_reporting为error_reporting = E_ALL & ~E_NOTICE 。这样程序中存在NOTICE和WARNING的不足之处的时候会被忽略,当然这对于新手来说并不合适,不仅不方便调试程序,而且不利于养成好的代码习惯。
    第二种方法:对每个变量进行初始化
    赋空值或者任意值即可,不影响运行。这对于变量比较多的程序来说比较繁琐,不过如果每次都提前设置下变量也是不错的习惯,例如:
      

  4.   

    你不是用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可能会有帮助。