配置成功,当微信用户向测试账号发消息时, 使用网页调试工具,显示请求失败
详细信息:
本人写了一个component(已经在配置文件中web.php成功加载),
配置代码如下:
详细信息:
本人写了一个component(已经在配置文件中web.php成功加载),
配置代码如下:
"components" => [ "weixinMessage" => [ "class" => "app\components\Weixin", ], ... ...
自定义的component Weixin.php 代码如下:
namespace app\components; use yii\base\Component; use yii\base\InvalidConfigException; define("TOKEN", "weixin"); class Weixin extends Component { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsgs() { //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->FromUserName; $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; } } public 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; } } }
本人用的是yii framework 2.0 basic template, 在action里面直接调用component里的valid()方法,配置成功,但使用网页测试工具传送文本消息,显示请求失败,controller为siteController, action为actionWeixin, 调用url为(http://example.com/web/index.php?r=site/weixin)
在controller中是这么调用的
public function actionWeixin() { Yii::$app->weixinMessage->valid(); Yii::$app->weixinMessage->responseMsgs(); }
本人发现,假如不MVC框架,直接把代码放在网站根目录下,直接访问(例如 http://example.com/weixin.php),使用网页调试工具,文本消息,请求成功,并且正确返回数据。代码如下。
define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid(); $wechatObj->responseMsgs(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsgs() { //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; } } }
解决方案
75
应该是跨域访问的问题。把csrf 验证关闭即可
5
楼上正解。