微信公衆號開發:自動回覆文本/圖片/圖文消息,關鍵詞回覆消息

對接流程

一、申請微信公衆號測試帳號
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網站

相關文章
相關標籤/搜索