开发者社区> 问答> 正文

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


服务器配置已经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;
	}
}

}

 

展开
收起
kun坤 2020-06-03 14:40:15 514 0
1 条回答
写回答
取消 提交回答
  • http://www.cnblogs.com/shendiao/archive/2013/04/24/3041450.html 
    //因为很多都设置了register_globals禁止,不能用$GLOBALS["HTTP_RAW_POST_DATA"];
    ######接口过了没######

    引用来自“要疼爱佳旻”的评论

    接口过了没

    服务器配置(已启用)

    ######打印一下$postStr ,有值吗######

    引用来自“shphper”的评论

    打印一下$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-06-04 10:33:54
    赞同 展开评论 打赏
问答分类:
PHP
问答地址:
问答排行榜
最热
最新

相关电子书

更多
微信广告引擎与播放节奏算法实践 立即下载
微信H5视频播放器在海量业务下的实践 立即下载
微信高效运维之路 立即下载