微信开发——连接接口与自动回复

  1. [php]  view plain  copy
    1. 首先我们在连接接口的时候,为了方便我们可以去微信公众号的开发者工具里用测试号进行测试,在写代码的过程中一定要谨记自己在配置接口信息的时候,接口信息中  
    2. 的token与代码中的token必须完全一致。   
    3. 例如:  
    4. 1.配置信息的代码  
    5.   
    6. <?php  
    7. /** 
    8.   * wechat php test 
    9.   */  
    10.   
    11.   
    12. //define your token  
    13. define("TOKEN""weixin");  
    14. $wechatObj = new wechatCallbackapiTest();  
    15. $wechatObj->valid();  
    16.   
    17.   
    18. class wechatCallbackapiTest  
    19. {  
    20.     public function valid()  
    21.     {  
    22.         $echoStr = $_GET["echostr"];  
    23.   
    24.   
    25.         //valid signature , option  
    26.         if($this->checkSignature()){  
    27.             echo $echoStr;  
    28.             exit;  
    29.         }  
    30.     }  
    31.   
    32.   
    33.     public function responseMsg()  
    34.     {  
    35.         //get post data, May be due to the different environments  
    36.         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  
    37.   
    38.   
    39.         //extract post data  
    40.         if (!empty($postStr)){  
    41.                   
    42.                 $postObj = simplexml_load_string($postStr'SimpleXMLElement', LIBXML_NOCDATA);  
    43.                 $fromUsername = $postObj->FromUserName;  
    44.                 $toUsername = $postObj->ToUserName;  
    45.                 $keyword = trim($postObj->Content);  
    46.                 $time = time();  
    47.                 $textTpl = "<xml>  
    48.                             <ToUserName><![CDATA[%s]]></ToUserName>  
    49.                             <FromUserName><![CDATA[%s]]></FromUserName>  
    50.                             <CreateTime>%s</CreateTime>  
    51.                             <MsgType><![CDATA[%s]]></MsgType>  
    52.                             <Content><![CDATA[%s]]></Content>  
    53.                             <FuncFlag>0</FuncFlag>  
    54.                             </xml>";               
    55.                 if(!empty$keyword ))  
    56.                 {  
    57.                     $msgType = "text";  
    58.                     $contentStr = "Welcome to wechat world!";  
    59.                     $resultStr = sprintf($textTpl$fromUsername$toUsername$time$msgType$contentStr);  
    60.                     echo $resultStr;  
    61.                 }else{  
    62.                     echo "Input something...";  
    63.                 }  
    64.   
    65.   
    66.         }else {  
    67.             echo "";  
    68.             exit;  
    69.         }  
    70.     }  
    71.           
    72.     private function checkSignature()  
    73.     {  
    74.         $signature = $_GET["signature"];  
    75.         $timestamp = $_GET["timestamp"];  
    76.         $nonce = $_GET["nonce"];      
    77.                   
    78.         $token = TOKEN;  
    79.         $tmpArr = array($token$timestamp$nonce);  
    80.         sort($tmpArr);  
    81.         $tmpStr = implode( $tmpArr );  
    82.         $tmpStr = sha1( $tmpStr );  
    83.           
    84.         if$tmpStr == $signature ){  
    85.             return true;  
    86.         }else{  
    87.             return false;  
    88.         }  
    89.     }  
    90. }  
    91.   
    92.   
    93. ?>  
    94. 2.自动回复(你可以根据关键字来回复你。例如:你输入天气,则回复你:今天天气阴天,请注意保暖!;输入你好,则回复你,hello)  
    95. <?php  
    96. /** 
    97.   * wechat php test 
    98.   */  
    99.   
    100.   
    101. //define your token  
    102. define("TOKEN""weixin");  
    103. $wechatObj = new wechatCallbackapiTest();  
    104. $wechatObj->valid();  
    105.   
    106.   
    107. class wechatCallbackapiTest  
    108. {  
    109.     public function valid()  
    110.     {  
    111.         if(isset($_GET['echostr']) && $this->checkSignature()){  
    112.             $echoStr = $_GET["echostr"];  
    113.             echo $echoStr;  
    114.             exit;  
    115.         }else{  
    116.             $this->responseMsg();  
    117.         }  
    118.           
    119.   
    120.   
    121.         //valid signature , option  
    122.         // if($this->checkSignature()){  
    123.               
    124.         // }  
    125.     }  
    126.   
    127.   
    128.     public function responseMsg()  
    129.     {  
    130.         //get post data, May be due to the different environments  
    131.         // $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  
    132.         $postStr = file_get_contents("php://input");  
    133.         file_put_contents("msg.txt"$postStr,FILE_APPEND);  
    134.   
    135.   
    136.         //extract post data  
    137.         if (!empty($postStr)){  
    138.                   
    139.                 $postObj = simplexml_load_string($postStr'SimpleXMLElement', LIBXML_NOCDATA);  
    140.                 $fromUsername = $postObj->FromUserName;  
    141.                 $toUsername = $postObj->ToUserName;  
    142.                 $keyword = trim($postObj->Content);  
    143.                 $time = time();  
    144.                 $textTpl = "<xml>  
    145.                             <ToUserName><![CDATA[%s]]></ToUserName>  
    146.                             <FromUserName><![CDATA[%s]]></FromUserName>  
    147.                             <CreateTime>%s</CreateTime>  
    148.                             <MsgType><![CDATA[%s]]></MsgType>  
    149.                             <Content><![CDATA[%s]]></Content>  
    150.                             <FuncFlag>0</FuncFlag>  
    151.                             </xml>";               
    152.                 if(!empty$keyword ))  
    153.                 {  
    154.                     $msgType = "text";  
    155.                     $contentStr = $this->word($keyword);  
    156.                     $resultStr = sprintf($textTpl$fromUsername$toUsername$time$msgType$contentStr);  
    157.                     echo $resultStr;  
    158.                 }else{  
    159.                     echo "Input something...";  
    160.                 }  
    161.   
    162.   
    163.         }else {  
    164.             echo "";  
    165.             exit;  
    166.         }  
    167.     }  
    168.     public function word($keyword){  
    169.         if($keyword=='天气'){  
    170.             $content="今天天气阴天,请注意保暖!";  
    171.             return $content;  
    172.         }else if($keyword=='你好'){  
    173.             $content='Hello';  
    174.             return $content;  
    175.         }else{  
    176.             $content="I don't know";  
    177.             return $content;  
    178.         }  
    179.     }  
    180.           
    181.     private function checkSignature()  
    182.     {  
    183.         $signature = $_GET["signature"];  
    184.         $timestamp = $_GET["timestamp"];  
    185.         $nonce = $_GET["nonce"];      
    186.                   
    187.         $token = TOKEN;  
    188.         $tmpArr = array($token$timestamp$nonce);  
    189.         sort($tmpArr);  
    190.         $tmpStr = implode( $tmpArr );  
    191.         $tmpStr = sha1( $tmpStr );  
    192.           
    193.         if$tmpStr == $signature ){  
    194.             return true;  
    195.         }else{  
    196.             return false;  
    197.         }  
    198.     }  
    199. }  
    200. echo 123;  
    201. ?>  

猜你喜欢

转载自blog.csdn.net/SongOkoK/article/details/79924073