微信公众号开发:自动回复文本/图片/图文消息,关键词回复消息

对接流程

一、申请微信公众号测试帐号
URL:https://mp.weixin.qq.com/debu...php

二、登陆,配置开发者服务器URL和Token服务器

image.png

开发者服务器配置代码:
config.php微信

<?php
define("TOKEN", "weixin"); //TOKEN值
$wechatObj = new wechat();
$wechatObj->valid();
class wechat {
  public function valid() {
    $echoStr = $_GET["echostr"];
    if($this->checkSignature()){
      echo $echoStr;
      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;
    }
  }
}
?>

URL是config.php在你服务器的URL
Token是上面代码本身设置的Token
image.png网络

搞定以后,就能完成接口配置的绑定。post

开发

清空config.php的代码,粘贴下方代码,保存。测试

<?php
  $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //获取POST数据
  //用SimpleXML解析POST过来的XML数据
  $postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
  $fromUsername = $postObj->FromUserName; //获取发送方账号(OpenID)
  $toUsername = $postObj->ToUserName; //获取接收方帐号
  $keyword = trim($postObj->Content); //获取消息内容
  $masType = $postObj->MsgType;//获取消息类型,能够做分类判断。本例默认是文本消息,不作判断
  $time = time(); //获取当前时间戳
   
  //文本消息模板
  $textTpl = "<xml>
              <ToUserName><![CDATA[%s]]></ToUserName>
              <FromUserName><![CDATA[%s]]></FromUserName>
              <CreateTime>%s</CreateTime>
              <MsgType><![CDATA[%s]]></MsgType>
              <Content><![CDATA[%s]]></Content>
              </xml>";

  //图片消息模板
  $imageTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Image>
            <MediaId><![CDATA[%s]]></MediaId>
            </Image>
            </xml>";

  //图片素材ID
  $media_id = "eO5xh-eLnU2o9K4waK2zmqpPEEEhblU4zQOCLbacNftUnZhTA2GR7G5QdKQt0rzq";

  //图文消息模板
  $newsTpl = "<xml>
              <ToUserName><![CDATA[%s]]></ToUserName>
              <FromUserName><![CDATA[%s]]></FromUserName>
              <CreateTime>%s</CreateTime>
              <MsgType><![CDATA[%s]]></MsgType>
              <ArticleCount>1</ArticleCount>
              <Articles>
              <item>
              <Title><![CDATA[%s]]></Title>
              <Description><![CDATA[%s]]></Description>
              <PicUrl><![CDATA[%s]]></PicUrl>
              <Url><![CDATA[%s]]></Url>
              </item>
              </Articles>
              </xml>";

  //图文配置
  $Title = "里客云资源站,分享各类网络资源,软件资源,影视资源,电脑资源!";
  $Description = "资源分享网站,免费下载各种资源";
  $PicUrl = "https://ae01.alicdn.com/kf/Hcec9e25f56424bf3a6516a4b9c5cc9e61.png";
  $Url = "http://www.likeyunba.com/";

  //判断要发送的类型
  if ($keyword == "文本") {
    $msgType = "text"; //文本类型
    $contentStr = "里客云资源站";
    $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
  }else if ($keyword == "图片") {
    $msgType = "image"; //图片类型
    $resultStr = sprintf($imageTpl,$fromUsername,$toUsername,$time,$msgType, $media_id);
  }else if ($keyword == "图文") {
    $msgType = "news"; //图文类型
    $resultStr = sprintf($newsTpl,$fromUsername,$toUsername,$time,$msgType,$Title,$Description,$PicUrl,$Url);
  }
  echo $resultStr;
?>

运行

image.png

但愿帮到您。

WeChat:face6009
Web:likeyunba.com
Date:2019-12-20
Author:Tanking网站

相关文章
相关标签/搜索