-
- 首先我们在连接接口的时候,为了方便我们可以去微信公众号的开发者工具里用测试号进行测试,在写代码的过程中一定要谨记自己在配置接口信息的时候,接口信息中
- 的token与代码中的token必须完全一致。
- 例如:
- 1.配置信息的代码
- <?php
- /**
- * wechat php test
- */
- //define your token
- define("TOKEN", "weixin");
- $wechatObj = new wechatCallbackapiTest();
- $wechatObj->valid();
- 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;
- }
- }
- }
- ?>
- 2.自动回复(你可以根据关键字来回复你。例如:你输入天气,则回复你:今天天气阴天,请注意保暖!;输入你好,则回复你,hello)
- <?php
- /**
- * wechat php test
- */
- //define your token
- define("TOKEN", "weixin");
- $wechatObj = new wechatCallbackapiTest();
- $wechatObj->valid();
- class wechatCallbackapiTest
- {
- public function valid()
- {
- if(isset($_GET['echostr']) && $this->checkSignature()){
- $echoStr = $_GET["echostr"];
- echo $echoStr;
- exit;
- }else{
- $this->responseMsg();
- }
- //valid signature , option
- // if($this->checkSignature()){
- // }
- }
- public function responseMsg()
- {
- //get post data, May be due to the different environments
- // $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
- $postStr = file_get_contents("php://input");
- file_put_contents("msg.txt", $postStr,FILE_APPEND);
- //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 = $this->word($keyword);
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- echo $resultStr;
- }else{
- echo "Input something...";
- }
- }else {
- echo "";
- exit;
- }
- }
- public function word($keyword){
- if($keyword=='天气'){
- $content="今天天气阴天,请注意保暖!";
- return $content;
- }else if($keyword=='你好'){
- $content='Hello';
- return $content;
- }else{
- $content="I don't know";
- return $content;
- }
- }
- 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;
- }
- }
- }
- echo 123;
- ?>
微信开发——连接接口与自动回复
猜你喜欢
转载自blog.csdn.net/SongOkoK/article/details/79924073
今日推荐
周排行