| 微信 平台 消息 接口 启用作者:http://txw1958.cnblogs.com/
 
 本系统教程以微信公众平台应用天气神(账号WeatherGod,支持国内近2500个县市区天气查询)为例,讲解微信接口开发过程。欢迎大家关注该账号,二维码见底部图。 使用前提条件:拥有一个公网上的HTTP服务器主机空间,具有创建目录、上传文件等权限。免费的也可以,但须有二级域名。想快速的申请一个免费稳定好用的,可以先使用新浪的SAE。但免费时间有条件限制。 
 一、注册微信公平平台账号地址:http://mp.weixin.qq.com/
 过程略。
 
 二、上传代码 以下代码是消息接口认证代码,将下列PHP代码保存为index.php(其中的mytoken改为你想要的的token名称),并上传到HTTP服务器指定的目录中, 
 复制代码<?php
/**
  * wechat php test
  */
//define your token
define("TOKEN", "wspd");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
$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)){
                
                      $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()
        {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];        
                        
                $token = TOKEN;
                $tmpArr = array($token, $timestamp, $nonce);
                sort($tmpArr);
                $tmpStr = implode( $tmpArr );
                $tmpStr = sha1( $tmpStr );
                
                if( $tmpStr == $signature ){
                        return true;
                }else{
                        return false;
                }
        }
}
?>
 
 或者直接下载微信的示例代码。地址 http://mp.weixin.qq.com/mpres/htmledition/res/wx_sample.zip 
 二、启用消息接口进入微信公众平台,选择 设置 - 关键词自动回复 ,选择启用 ,点击 公众平台消息接口 旁边的 编辑,进入接口配置界面:
 按照要求如实填写下列选项。特别注意是URL是你上传index.php的路径,必须以http://开头,且为80端口。
 填好后点击提交,这会有下面这些结果1. 服务器没有响应Token验证
 这样回头检查一下各项配置是否正确。如果确定配置没有问题,请按下面的方法检查 2. 请求URL超时 你的服务器在国外,或者服务器网速不给力,一般多试几次就可以了。如果经常这样,就需要考虑更换服务器 3. 提交成功 恭喜你,配置成功了。 
 三、服务器没有响应Token验证的解决方法 
 原文地址:http://www.cnblogs.com/txw1958/archive/2013/01/24/weixin-if1-enable.html 
 关注天气神(账号WeatherGod)方法: 1. 依次进入以下路径:朋友们—>添加朋友—>搜号码,输入WeatherGod,不区分大小写,点击查找,然后点击关注。 2. 扫描二维码: 
 |