PHP轻松实现BAE的微信公众账号关注回复图文信息

微信公众账号已有很多公司在使用,如何在被关注后回复图文信息呢?方法还是比较简单,首先在微信公众账号web登录,创建素材,然后将素材图片的链接、素材链接、标题记下来,放在回复的代码中,代码存放的位置可以是你自已的域名某目下,也可以放在百度开发者中心的BAE中,我用的是百度应用中心的,以下是具体的实现代码!

[php][/php] 

  1. <?php
  2. /*
  3.     sexoffice
  4.     CopyRight 2013  baronyang
  5. */
  6. define(“TOKEN”, “sexoffice”);
  7. $wechatObj = new wechatCallbackapiTest();
  8. $wechatObj->responseMsg();
  9. class wechatCallbackapiTest
  10. {
  11.   private $fromUsername;
  12.   private $toUsername;
  13.   private $keywordl;
  14.   private $time;
  15.   private $MsgType;
  16.   private $EventType;
  17.   //回复文本信息 XML是回复的格式
  18.   private function  ReplyTextMsg($sendmsg){
  19.             $textTpl = “<xml>
  20.                         <ToUserName><![CDATA[%s]]></ToUserName>
  21.                         <FromUserName><![CDATA[%s]]></FromUserName>
  22.                         <CreateTime>%s</CreateTime>
  23.                         <MsgType><![CDATA[%s]]></MsgType>
  24.                         <Content><![CDATA[%s]]></Content>
  25.                         <FuncFlag>0</FuncFlag>
  26.                         </xml>”;
  27.          $resultStr = sprintf($textTpl, $this->fromUsername, $this->toUsername, $this->time, ‘text’, $sendmsg);
  28.          echo $resultStr;
  29.   }
  30.    //关注后回复图文信息
  31.   private function AttentionReply1(){
  32.      $textTpl=” <xml>
  33.                     <ToUserName><![CDATA[%s]]></ToUserName>
  34.                     <FromUserName><![CDATA[%s]]></FromUserName>
  35.                     <CreateTime>%s</CreateTime>
  36.                     <MsgType><![CDATA[news]]></MsgType>
  37.                     <ArticleCount>1</ArticleCount>
  38.                     <Articles>
  39.                         <item>
  40.                             <Title><![CDATA[%s]]></Title>
  41.                             <Description><![CDATA[%s]]></Description>
  42.                             <PicUrl><![CDATA[%s]]></PicUrl>
  43.                             <Url><![CDATA[%s]]></Url>
  44.                         </item>
  45.                     </Articles>
  46.                     <FuncFlag>1</FuncFlag>
  47.                 </xml>”;
  48.         $title1=”道具研究所欢迎您”;
  49.         $Description1=”亲,欢迎您关注道具研究所,本研究所将于每天18:00-22:00点发布精品图文,”;
  50.         $Description1.=”大部份内容会涉及夫妻生活及情趣讨论,成人道具介绍,情趣内衣欣赏,AV女优推荐,性生活故事,道具使用感受分享,等敏感话题……。”;
  51.         $PicUrl1=”http://mmsns.qpic.cn/mmsns/q3PibibJOcnbtgicUHMlbnaKwufAUw4SrvY6nlwlu3PhQXtkn4FoIytmg/0″;
  52.         $Url1=”http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5OTkxNjIwMw==&appmsgid=10000050&itemidx=1&sign=cf074e4612e18689a0c79d8f5555f791#wechat_redirect”;
  53.         $resultStr = sprintf($textTpl, $this->fromUsername, $this->toUsername, $this->time,$title1,$Description1,$PicUrl1,$Url1);
  54.         echo $resultStr;
  55.   }
  56.   //关注后回复图文信息
  57.   private function AttentionReply2(){
  58.      $textTpl=” <xml>
  59.                     <ToUserName><![CDATA[%s]]></ToUserName>
  60.                     <FromUserName><![CDATA[%s]]></FromUserName>
  61.                     <CreateTime>%s</CreateTime>
  62.                     <MsgType><![CDATA[news]]></MsgType>
  63.                     <ArticleCount>3</ArticleCount>
  64.                     <Articles>
  65.                         <item>
  66.                             <Title><![CDATA[%s]]></Title>
  67.                             <Description><![CDATA[%s]]></Description>
  68.                             <PicUrl><![CDATA[%s]]></PicUrl>
  69.                             <Url><![CDATA[%s]]></Url>
  70.                         </item>
  71.                         <item>
  72.                             <Title><![CDATA[%s]]></Title>
  73.                             <Description><![CDATA[%s]]></Description>
  74.                             <PicUrl><![CDATA[%s]]></PicUrl>
  75.                             <Url><![CDATA[%s]]></Url>
  76.                         </item>
  77.                         <item>
  78.                             <Title><![CDATA[%s]]></Title>
  79.                             <Description><![CDATA[%s]]></Description>
  80.                             <PicUrl><![CDATA[%s]]></PicUrl>
  81.                             <Url><![CDATA[%s]]></Url>
  82.                         </item>
  83.                     </Articles>
  84.                     <FuncFlag>1</FuncFlag>
  85.                 </xml>”;
  86.         $title1=”女人巧用口与舌头让男人忍不住叫喊起来”;
  87.         $Description1=”1″;
  88.         $PicUrl1=”http://mmsns.qpic.cn/mmsns/q3PibibJOcnbtCk2sdBRPzrMAjSicIDzt8dXxdeMjsYOr4ErOPErpSsRA/0″;
  89.         $Url1=”http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5OTkxNjIwMw==&appmsgid=10000044&itemidx=1&sign=e888bafa310611f0d8ad9c5f979316f0#wechat_redirect”;
  90.         $title2=”让男人欲罢不能的内衣”;
  91.         $Description2=”2″;
  92.         $PicUrl2=”http://mmsns.qpic.cn/mmsns/q3PibibJOcnbtCk2sdBRPzrMAjSicIDzt8d58pYI850oAn7ibjWib8IhyGg/0″;
  93.         $Url2=”http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5OTkxNjIwMw==&appmsgid=10000044&itemidx=2&sign=85285c0119705e81b75055315953563f#wechat_redirect”;
  94.         $title3=”我用身体记录我们相识的记忆”;
  95.         $Description3=”3″;
  96.         $PicUrl3=”http://mmsns.qpic.cn/mmsns/q3PibibJOcnbtCk2sdBRPzrMAjSicIDzt8dcYUh7OKCuCvicTQaSOicypwg/0″;
  97.         $Url3=”http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5OTkxNjIwMw==&appmsgid=10000044&itemidx=3&sign=e615e719436cb3efc6d61d2dcb938dd5#wechat_redirect”;
  98.         $resultStr = sprintf($textTpl, $this->fromUsername, $this->toUsername, $this->time,$title1,$Description1,$PicUrl1,$Url1,
  99.                              $title2,$Description2,$PicUrl2,$Url2,$title3,$Description3,$PicUrl3,$Url3);
  100.         echo $resultStr;
  101.   }
  102.   //定制回复信息
  103.   public function responseMsg()  {  //取用户数据
  104.         $postStr = $GLOBALS[“HTTP_RAW_POST_DATA”];
  105.         if (!empty($postStr)){
  106.             $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement’, LIBXML_NOCDATA);
  107.             $this->fromUsername = $postObj->FromUserName;//用户的微信号加密ID
  108.             $this->toUsername = $postObj->ToUserName;    //开发者ID
  109.             $this->keyword = trim($postObj->Content);
  110.             $this->MsgType=utf8_decode($postObj->MsgType);            //消息类型
  111.             if (property_exists($postObj,”Event”)==true) {
  112.                 $this->EventType=utf8_decode($postObj->Event);
  113.             }
  114.             $this->time = time();
  115.             if($this->MsgType == “text”){
  116.               if (utf8_decode(trim($this->keyword))==”?”){
  117.                   $contentStr=” 亲!您想了解哪个哪类型的问题呢?A情趣内衣、B成人玩具还是其他?”;
  118.                   $this->ReplyTextMsg($contentStr);
  119.                   exit;
  120.               }
  121.               if (utf8_decode(trim($this->keyword))==”A”){
  122.                   $contentStr =$this->fromUsername.” 情趣内衣让您的生活更有情趣,详情请了解”;
  123.                   $this->ReplyTextMsg($contentStr);
  124.                   $this->ReplyTextMsg(“test”);
  125.                   exit;
  126.               }
  127.             }
  128.           if ($this->MsgType==’event’){
  129.             //当公众账号被关注后,自动回复图文信息
  130.             if ($this->EventType==’subscribe’){
  131.               //$this->AttentionReply2(); 本来想回复两条的,结果微信只能回复一条,屏蔽了这一条
  132.                $this->AttentionReply1();
  133.                exit;
  134.             }
  135.           }
  136.         }
  137.     }
  138.  }
  139. ?>

以上是完整的代码了,要看回复的效果,可以用搜公众账号sexoffice,点一下关注,然后点查看消息,就能看到图文回复了。可以将sexoffice换成你的微信公众账号,相关链接也换成你的,就可以用了。

 

可惜微信暂时不支持在用代码给微信账号发图文信息!

标签