开发者社区> 问答> 正文

php配置微信平台接口遇到的问题,报错

image.png

服务器配置已经OK,然后将$wechatObj->valid()  修改成 $wechatObj->responseMsg() !

在微信里给公众账号发信息没有信息返回?不知道为什么?

define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();

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;
		}
	}
}

展开
收起
一枚小鲜肉帅哥 2020-05-28 09:22:07 817 0
1 条回答
写回答
取消 提交回答
  • 菜不可及的程序猿

    "

    <span style=""color:#000000;""><a href=""http://www.cnblogs.com/shendiao/archive/2013/04/24/3041450.html"" rel=""nofollow"">http://www.cnblogs.com/shendiao/archive/2013/04/24/3041450.html 

    //因为很多都设置了register_globals禁止,不能用$GLOBALS["HTTP_RAW_POST_DATA"];
    接口过了没

    服务器配置(已启用)

    ######打印一下$postStr ,有值吗 $postxml = file_get_contents('php://input'); 这样能获取到xml包了! 是没有post xml包的问题吗? 有知道为什么的不? 把echo 改为 return

    现在的疑惑是

    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];



    获取不到post的内容

    现在改成

    $postxml = file_get_contents('php://input');



    就能获取到post的内容了

    ######哈哈######你是不是用了PHP 6######

    引用来自“每天打起精神即可”的评论

    http://www.cnblogs.com/shendiao/archive/2013/04/24/3041450.html 
    //因为很多都设置了register_globals禁止,不能用$GLOBALS["HTTP_RAW_POST_DATA"];
    非常感谢!######没什么问题啊   你重新关注下  或者退出下微信
    2020-05-29 13:59:55
    赞同 展开评论 打赏
问答分类:
PHP
问答地址:
问答排行榜
最热
最新

相关电子书

更多
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载