USEGEAR

导航

微信练手-环境准备(1)

微信一大好处就是不用下载app,就能直接通过公众号关注你,从而拉进拉紧你关心的人,包括客户。

微信实际上就是一电话总机中转站,仅仅做一点p事。所以别看的多神奇。

第一步:申请公众号(网上一大把);

要说的是,前期测试而言,不要三百元大洋审核。用开发者菜单中的测试平台。所有的审核收费功能在这里都提供。

第二部:配置服务器:开发者中心有配置项。

这里的wxapi.php怎么写:官方已经提供了一个demo,define("TOKEN", "weixin");。这里要和配置中的Token保持一致。然后修改该demo文件名,再然后上传到你的五花八门的域名网站上即可。

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

第三:提交保存:确认前面几部没有错误,多保存几次。因为没你想象的反应那么快。最后一定是成功。

 

posted on 2015-06-02 14:59  USEGEAR  阅读(211)  评论(0编辑  收藏  举报