php - 微信公众号开发xml:Xml fields error, please check your response xml!
高洛峰
高洛峰 2017-04-11 10:04:26
0
3
1211

php代码如下 tp框架

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function wechat(){
      define("TOKEN", "wujindong");
      //1,将timestamp,nonce,token按字典序排序
      //2,将排序后的参数sha1加密
      //3,将加密后的参数和signature对比
      $token = TOKEN;
      $signature = $_GET["signature"];
      $timestamp = $_GET["timestamp"];
      $nonce = $_GET["nonce"];
      $echoStr = $_GET["echostr"];
      $array = array($token, $timestamp, $nonce);
          sort($array, SORT_STRING);
      $tmpstr = implode($array);
      $tmpstr = sha1($tmpstr);
      if ($tmpstr==$signature && $echoStr) {
            echo $echoStr;
            exit;
      }else {
        $this->responseMsg();
      }
    }
    public function responseMsg()
    {
    //get post data, May be due to the different environments
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    // $tmpstr = $postStr;
    libxml_disable_entity_loader(true);
    $postObj = simplexml_load_string($postStr,'SimpleXMLElement', LIBXML_NOCDATA);
    //extract post data
    if (strtolower( $postObj->MsgType ) == 'event'){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                   if (strtolower( $postObj->Event == 'subscribe')) {
                     $toUsername = $postObj->FromUserName;
                     $fromUsername = $postObj->ToUserName;
                     $time = time();
                     $MsgType = 'text';
                     $content = "欢迎访问我的公众号";
                     $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Event><![CDATA[%s]]></Event></xml>";
                     $info = sprintf($textTpl,$toUsername,$fromUsername,$time,$MsgType,$content);
                     echo $info;
                   }
        }
    }
}



测试结果如下:






http://120.27.115.59/wechat.php/Home/Index/wechat
成功或失败:
Decrypt Fail!  
错误原因:
Xml fields error, please check your response xml! 
状态码:
200    OK 
Headers:
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Date: Tue, 23 Aug 2016 10:23:34 GMT
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Set-Cookie: PHPSESSID=4tlrj4jd7cgoo3ueaigc6753q7; path=/
Pragma: no-cache
Server: Apache/2.4.7 (Ubuntu)
Vary: Accept-Encoding
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Content-Type: text/html
Content-Length: 239
 
解密前xml:
<xml>
    <ToUserName>
        <![CDATA[wujindong1120]]>
    </ToUserName>
    <FromUserName>
        <![CDATA[wujindong1120]]>
    </FromUserName>
    <CreateTime>1471947814</CreateTime>
    <MsgType>
        <![CDATA[text]]>
    </MsgType>
    <Event>
        <![CDATA[欢迎访问我的公众号]]>
    </Event>
</xml>
 
解密后xml:
 
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
小葫芦

解决了。。貌似是tp的问题

  • reply Hello, I also encountered this problem. Never been able to solve it. What is the problem with TP?
    流影 author 2018-01-19 11:09:21
伊谢尔伦

我也遇到了,不是用tp写的,用的原生。怎么搞呢

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template