基于PHP的微信公众号的开发流程详解

吾爱主题 阅读:137 2021-10-21 14:12:00 评论:0

微信公众号开发分傻瓜模式和开发者模式两种,前者不要考虑调用某些接口,只要根据后台提示傻瓜式操作即可,适用于非专业开发人员。

开发模式当然就是懂程序开发的人员使用的。

下面简单说一下微信公众号开发的简易流程,新手看看会有帮助,高手请一笑而过。

1、配置服务器:

  a、首先在本机建立如下结构的文件夹(这里是我自己的习惯,仅供参考)

mmpn:总目录mro message public number 微信公众号

backup:备份目录,主要用于备份php文件,每次修改时将原稿备份到里面去。

images:存放图片

includes:包含文件,包括mysql配置,smarty模板包含文件等等

menu:存放公众号菜单信息,txt文档即可

gr****zx.php:开发文件,即公众号关联的程序文件,也就是配置的url对应的文件

最后的是需求等等的文件

主程序文件:gr****zx.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 <?php      define( "token" , "aabbc_lzpt" );      //获取微信发送数据    $poststr = $globals [ "http_raw_post_data" ];    //返回回复数据    if (! empty ( $poststr ))    {      //解析数据      $postobj = simplexml_load_string( $poststr , 'simplexmlelement' , libxml_nocdata);      //发送消息方id      $fromusername = $postobj ->fromusername;      //接送消息方id      $tousername = $postobj ->tousername;      //消息类型      $form_msgtype = $postobj ->msgtype;        //事件消息      if ( $form_msgtype == "event" )      {        //获取事件类型        $form_event = $postobj ->event;        //订阅事件        if ( $form_event == "subscribe" )        {            //回复欢迎图文信息          /*          $resultstr = "<xml>                  <tousername><![cdata[".$fromusername."]]></tousername>                  <fromusername><![cdata[".$tousername."]]></fromusername>                  <createtime>".time()."</createtime>                  <msgtype><![cdata[news]]></msgtype>                  <articlecount>2</articlecount>                  <articles>                    <item>                      <title><![cdata[ 欢迎关注***微信服务平台,****]]></title>                      <description><![cdata[这是简短描述文字]]></description>                      <picurl><![cdata[http://a.hiphotos.baidu.com/baike/c0%3dbaike116%2c5%2c5%2c116%2c38/sign=5cae7405f21f3a294ec5dd9cf84cd754/32fa828ba61ea8d32de5a1df950a304e241f5822.jpg]]></picurl>                      <url><![cdata[http://www.baidu.com]]></url> </item>                    <item>                      <title><![cdata[最新动态]]></title>                      <description><![cdata[]]></description>                      <picurl><![cdata[http://a.hiphotos.baidu.com/baike/c0%3dbaike116%2c5%2c5%2c116%2c38/sign=5cae7405f21f3a294ec5dd9cf84cd754/32fa828ba61ea8d32de5a1df950a304e241f5822.jpg]]></picurl>                      <url><![cdata[http://www.baidu.com]]></url> </item>                    </articles>                  </xml> ";          */          //回复欢迎文字信息            $reply = "您好,欢迎关注******微信公众平台" ;          $resultstr ="<xml>                  <tousername><![cdata[ ".$fromusername." ]]></tousername>                  <fromusername><![cdata[ ".$tousername." ]]></fromusername>                  <createtime> ".time()." </createtime>                  <msgtype><![cdata[text]]></msgtype>                  <content><![cdata[ ".$reply." ]]></content>                </xml>";        }        else if ( $form_event == "click" )        {          $form_event_key = $postobj ->eventkey;          if ( $form_event_key == "v3002_contact" )          {            /*             $resultstr = "<xml>                  <tousername><![cdata[".$fromusername."]]></tousername>                  <fromusername><![cdata[".$tousername."]]></fromusername>                  <createtime>".time()."</createtime>                  <msgtype><![cdata[news]]></msgtype>                  <articlecount>1</articlecount>                  <articles>                    <item>                      <title><![cdata[如何成为本站会员]]></title>                      <description><![cdata[本栏目介绍详细的加入流程!]]></description>                      <picurl><![cdata[http://a.hiphotos.baidu.com/baike/c0%3dbaike116%2c5%2c5%2c116%2c38/sign=5cae7405f21f3a294ec5dd9cf84cd754/32fa828ba61ea8d32de5a1df950a304e241f5822.jpg]]></picurl>                      <url><![cdata[http://www.baidu.com]]></url>                    </item>                  </articles>                  </xml> ";            */            $reply = "我们的地址:黄河东路222号\n我们的热线:0510-88888888" ;            $resultstr ="<xml>                  <tousername><![cdata[ ".$fromusername." ]]></tousername>                  <fromusername><![cdata[ ".$tousername." ]]></fromusername>                  <createtime> ".time()." </createtime>                  <msgtype><![cdata[text]]></msgtype>                  <content><![cdata[ ".$reply." ]]></content>                </xml>";          }        }        }      else if ( $form_msgtype == "location" ){        $lng1 = $postobj ->location_x;        $lat1 = $postobj ->location_y;        $from_location_label = $postobj ->label;        $reply = "地理位置:" ;        $reply .= "纬度" . $lat1 . "\t经度" . $lng1 . "位置" . $from_location_label ;        $resultstr = "<xml>                <tousername><![cdata[ ".$fromusername." ]]></tousername>                <fromusername><![cdata[ ".$tousername." ]]></fromusername>                <createtime> ".time()." </createtime>                <msgtype><![cdata[text]]></msgtype>                <content><![cdata[ ".$reply." ]]></content>                </xml> ";        }      echo $resultstr ;      exit ;    }    else    {      echo "" ;      exit ;    } ?>

临时文件:wx_sample.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 <?php /**   * wechat php test   */   //define your token define( "token" , "aabbc_lzpt" ); $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 , sort_string);      $tmpstr = implode( $tmparr );      $tmpstr = sha1( $tmpstr );           if ( $tmpstr == $signature ){        return true;      } else {        return false;      }    } }   ?>

注意看两个文件开头都有代码:define("token", "aabbc_lzpt"); 这是定义token,后面的值自己设置,但是自己要记住,在微信后台配置服务器要用到它。

b、现在开始操作,首先,将gr****zx.php文件中的内容(代码),临时清空,代替以临时文件中的内容(代码),保存。将mmpn项目传到服务器,这时,主程序文件url为“http://www.****.com/mmpn/fy_hzx/grape_fy_hzx.php”这样的形式,确保路径正确,打开微信公众号后台开发者中心,点击下图中“修改配置”,将刚才的url填入相应控件

token就是刚才提到要记住的token的值,要一致。第三个“消息加密密钥”可以随机生成,然后提交。

token正确和url正确以及文件代码无误的话,会提示成功,再点启用。

c、刚才主程序文件中的代码是临时的,只是为了开通注册验证token,成功以后,可以再次清除,恢复成原来的内容。(保存好再上传到服务器)

这样服务器配置工作就结束了。

2、介绍一下主程序文件中的代码

  $form_msgtype = $postobj->msgtype;

  $form_msgtype消息类型,分为“事件”、“位置”(粉丝发送位置)、“文字类型”、“图片类型”等,具体可查看后台接口文档

  $form_event = $postobj->event;

  $form_event 事件类型分为“订阅”(subscribe)(也就是点关注)、退订(unsubscribe)、“点击”(click)——点击菜单,但是只针对click类型的菜单,如果是url类型的菜单,则直接打开对应的url,无须在代码中处理。

  回复粉丝的内容形式一般为文字或图文两种,代码中都有。

3、菜单配置

  菜单配置要知道开发者id中的appid(这个可以直接看到)和appsecret(这个有可能是被隐藏的,要通过公众号管理员验证通过申请查看才能显示完整,显示完整后要及时保存记录,这样下次就不用再找管理员了)。

  配置菜单前,首先要拿到access_token,它的有效期为2小时。如图点击获取access_token

然后在右下方点击“使用网页调试工具调试该接口”

输入appid和secret,点“检查问题”提交

拷贝获取到的access_token

重新选择自定义菜单:

将刚获取到的access_token 拷入

如果接口列表选择的是查询菜单,提交的结果是不存在,这就对了,因为我们还没有创建菜单呢

那么,我们就创建一个,将以下json格式代码拷入body中,提交

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 {      "button" : [        {          "name" : "首页" ,          "sub_button" : [            {              "type" : "view" ,              "name" : "注册登录" ,              "url" : "http://www.baidu.com" ,              "sub_button" : [ ]            },            {              "type" : "click" ,              "name" : "娱乐一刻" ,              "key" : "v1001_query" ,              "sub_button" : [ ]            },            {              "type" : "view" ,              "name" : "查看官网" ,              "url" : "http://www.baidu.com" ,              "sub_button" : [ ]            }                     ]        },        {          "name" : "主页" ,          "sub_button" : [           {              "type" : "view" ,              "name" : "注册登录" ,              "url" : "http://www.baidu.com" ,              "sub_button" : [ ]            },            {              "type" : "click" ,              "name" : "娱乐一刻" ,              "key" : "v1001_query" ,              "sub_button" : [ ]            },            {              "type" : "view" ,              "name" : "查看官网" ,              "url" : "http://www.baidu.com" ,              "sub_button" : [ ]            }                     ]        },        {          "name" : "关注我们" ,          "sub_button" : [            {              "type" : "view" ,              "name" : "注册登录" ,              "url" : "http://www.baidu.com" ,              "sub_button" : [ ]            },            {              "type" : "click" ,              "name" : "娱乐一刻" ,              "key" : "v1001_query" ,              "sub_button" : [ ]            },            {              "type" : "view" ,              "name" : "查看官网" ,              "url" : "http://www.baidu.com" ,              "sub_button" : [ ]            }          ]        }      ]    }

提交,提示成功菜单即生成。

  菜单内容中,有两个类型 (type),一个是view,这个会直接打开相应url链接;一个是click,这个就会在刚才主程序文件中寻找对应key值运行相关的代码。

  格式不要弄错就行。

  如果要修改菜单,先删除,再创建;一般来说,菜单更改后,公众号须重新关注才能马上看到更改效果,否则要等24小时自动更新。

  到此这篇关于基于php的微信公众号的开发流程详解的文章就介绍到这了,更多相关php微信公众号的开发内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/bribra/p/4236589.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容