搭建微信公众号后台开发环境

1.微信公众号

微信公众号逐渐成为很多公司的一种平台媒介,而微信公众号后台开发,是值得人学习的事务.

从一定意义上来说,微信公众号开发,就是另一种web服务器开发;不同的是,该web服务器开发,依托微信公众号平台,被微信进行了一定限制.

微信关于公众号开发的规范,有相应文档:https://mp.weixin.qq.com/

如果想学习微信公众号开发,需要首先对该文档进行大致学习.

2.微信公众号后台搭建

然后,可以搭建微信公众号后台.

后台搭建,需要申请服务器资源,需要有固定IP,一般来说,方法如下:

1)公司的固定IP;

2)各种云服务器的固定IP.

1)云服务器选择

对于开发者来说,可以用云服务的IP,至于是新浪云,百度云,阿里云,腾讯云,完全是自身选择.

我自己,用了腾讯云;该云需要实名认证,并可以免费开通云服务器 CVM,15天内免费体验(https://cloud.tencent.com/act/free).

我使用了centos7.2.

2)Nginx服务器安装

然后,设置完密码后,在云上安装了Nginx,安装方法参考https://www.cnblogs.com/zhoading/p/8514050.html

在Nginx进行configure的时候,我configure的路径为:/data/nginx,命令为./configure --prefix=/data/nginx

make 和 make install之后,安装成功.

cd 到/data/nginx/sbin下,./nginx 可以启动nginx, ./nginx -s stop可以关闭进程.

在IE输入自己的云服务器IP,可以看到有登录页,证明Nginx安装成功.

3)PHP安装

然后,可以在云上,对PHP服务器进行安装.安装参考https://www.cnblogs.com/lalalagq/p/9979068.html

需要注意的是.需要对Nginx的配置文件进行更改.

 location ~ \.php 部分原来为注释的,现在应该放开.

同时,建议加上include fastcgi.conf,这样可以避免Primary script unknown错误.

完整phplocation如下:

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
            include        fastcgi.conf;
        }

4)在微信公众号里,对开发进行设置.

登录微信公众号平台后,选择"基本配置-------------服务器配置",可以对服务器进行配置.

5)微信公众号 token配置失败问题

如果服务器没有配置好,会造成token配置失败问题.

原因为,微信公众号会登录,会验证url和token,如果token本身没有设置,会造成token配置失败问题一直存在.

配置token方法如下,在nginx的template下,新建index.php,并写下如下的php:

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "test");
$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>
                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;
        }
    }
}

echo "Hello, world";
?>

然后,在基本配置上,设置URL为自己的云服务器的地址,Token为test,如图所示:

 

 

这样,配置服务器设置成功

 

附:

微信开发参考文档:https://zhuanlan.zhihu.com/p/31157348

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值