微信公众平台开发-服务器配置提交失败问题解决

之前总是提交失败,找了很多资料,最终加了第16行代码(header('content-type:text');)通过验证,最终的验证脚本文件内容如下:

 1 <?php
 2 //define your token
 3 define("TOKEN", "kaigesoftweixin");
 4 $wechatObj = new wechatCallbackapiTest();
 5 $wechatObj->valid();
 6 
 7 class wechatCallbackapiTest
 8 {
 9     public function valid()
10     {
11         $echoStr = $_GET["echostr"];
12 
13         //valid signature , option
14         if($this->checkSignature()){
15             //如果验证失败,可以加上这一句
16             header('content-type:text');
17             echo $echoStr;
18             exit;
19         }
20         else{
21             echo "error!";
22             exit;
23         }
24     }
25 
26     public function responseMsg()
27     {
28         //get post data, May be due to the different environments
29         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
30 
31           //extract post data
32         if (!empty($postStr)){
33                 
34                   $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
35                 $fromUsername = $postObj->FromUserName;
36                 $toUsername = $postObj->ToUserName;
37                 $keyword = trim($postObj->Content);
38                 $time = time();
39                 $textTpl = "<xml>
40                             <ToUserName><![CDATA[%s]]></ToUserName>
41                             <FromUserName><![CDATA[%s]]></FromUserName>
42                             <CreateTime>%s</CreateTime>
43                             <MsgType><![CDATA[%s]]></MsgType>
44                             <Content><![CDATA[%s]]></Content>
45                             <FuncFlag>0</FuncFlag>
46                             </xml>";             
47                 if(!empty( $keyword ))
48                 {
49                       $msgType = "text";
50                     $contentStr = "Welcome to wechat world!";
51                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
52                     echo $resultStr;
53                 }else{
54                     echo "Input something...";
55                 }
56 
57         }else {
58             echo "";
59             exit;
60         }
61     }
62         
63     private function checkSignature()
64     {
65         $signature = $_GET["signature"];
66         $timestamp = $_GET["timestamp"];
67         $nonce = $_GET["nonce"];    
68                 
69         $token = TOKEN;
70         $tmpArr = array($token, $timestamp, $nonce);
71         sort($tmpArr);
72         $tmpStr = implode( $tmpArr );
73         $tmpStr = sha1( $tmpStr );
74         
75         if( $tmpStr == $signature ){
76             return true;
77         }else{
78             return false;

79         }
80     }
81 }
82 ?>
View Code

大家可以直接用这个代码,有需要可以把第三行

    define("TOKEN", "kaigesoftweixin");

中的"kaigesoftweixin" 变为自己想设的token值。

 

posted @ 2015-11-16 16:43  仁心之士  阅读(589)  评论(0编辑  收藏  举报