服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - PHP技术开发微信公众平台

PHP技术开发微信公众平台

2020-11-08 18:20PHP教程网 PHP教程

这篇文章主要介绍了微信公众平台的两种模式(编辑模式和开发模式)顾名思义编辑模式就是写普通的功能,开发模式具有更多的功能,下面主要是针对开发模式做介绍,需要的朋友可以参考下

下面通过图文并茂的方式介绍微信公众平台开发过程,具体内容如下:

微信公众平台有两种模式:编辑模式 和 开发模式。

普通的功能可以通过编辑模式来搞定。开发模式具有更多的功能。让我们来使用开发模式开发helloword吧

步骤如下:

第一步:先注册一个公众号(https://mp.weixin.qq.com)

第二步:注册sae(http://sae.sina.com.cn/),作为你的服务器。

第三步:登录微信公众平台(https://mp.weixin.qq.com)查看开发文档并下载官方提供的demo。做适当修改。

第四步:将代码压缩成zip格式,上传到sae平台。

第五步:登录微信公众平台,进入开发者中心。开启“服务者配置”。

第六步:成功了。

开始吧:

1.先注册一个公众号(https://mp.weixin.qq.com)

2.注册sae(http://sae.sina.com.cn/)

注册完以后要记得进行实名认证,不然绑定到公众平台的时候,会有永远的“无法获取token”的提示。(实名认证需要3个工作日才能成功)

然后可以点击创建应用。创建后可以在下面看到。

PHP技术开发微信公众平台

进入自己创建的应用。然后点击代码管理。

PHP技术开发微信公众平台

PHP技术开发微信公众平台

3.登录微信公众平台(https://mp.weixin.qq.com)

查看开发文档并下载官方提供的demo。

PHP技术开发微信公众平台

PHP技术开发微信公众平台

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
<?php
/**
 * wechat php test
 */
//define your token
define("token", "weixin");
$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)){
  /* libxml_disable_entity_loader is to prevent xml external entity injection,
   the best way is to check the validity of xml by yourself */
  libxml_disable_entity_loader(true);
   $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()
 {
 // you must define token by yourself
 if (!defined("token")) {
  throw new exception('token is not defined!');
 }
 $signature = $_get["signature"];
 $timestamp = $_get["timestamp"];
 $nonce = $_get["nonce"];
 $token = token;
 $tmparr = array($token, $timestamp, $nonce);
 // use sort_string rule
 sort($tmparr, sort_string);
 $tmpstr = implode( $tmparr );
 $tmpstr = sha1( $tmpstr );
 if( $tmpstr == $signature ){
  return true;
 }else{
  return false;
 }
 }
}
?>

我试过,如上代码,似乎无法执行到response那一块。所以做了更改

?
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
<?php
/**
 * wechat php test
 */
//define your token
define("token", "weixin");
$wechatobj = new wechatcallbackapitest();
//这里做了更改
if($_get["echostr"]){
 $wechatobj->valid();
}else{
 $wechatobj->responsemsg();
}
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)){
  /* libxml_disable_entity_loader is to prevent xml external entity injection,
   the best way is to check the validity of xml by yourself */
  libxml_disable_entity_loader(true);
   $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()
 {
 // you must define token by yourself
 if (!defined("token")) {
  throw new exception('token is not defined!');
 }
 $signature = $_get["signature"];
 $timestamp = $_get["timestamp"];
 $nonce = $_get["nonce"];
 $token = token;
 $tmparr = array($token, $timestamp, $nonce);
 // use sort_string rule
 sort($tmparr, sort_string);
 $tmpstr = implode( $tmparr );
 $tmpstr = sha1( $tmpstr );
 if( $tmpstr == $signature ){
  return true;
 }else{
  return false;
 }
 }
}
?>

你可以将welcome to wechat world!更改为hello word!

4.将代码压缩成zip格式,上传到sae平台。

PHP技术开发微信公众平台

点击“编辑代码”,可以看到你上传的php文件。然后右击,url查看。复制url(http://1.carlzhang.sinaapp.com/wx_carlzhang819.php)。在这里要记住在此php文件中定义的token。此处为“weixin”,可以在如下图中看到。

PHP技术开发微信公众平台

5.登录微信公众平台,进入开发者中心。开启“服务者配置”。url填写上一步复制的url(这里我删除了前面的1.因为我的sae默认第一版。你可以试试,删除1,若是url访问,不报404,那就是没问题)。token填写的是代码中的token(上面是“weixin”)。

PHP技术开发微信公众平台

PHP技术开发微信公众平台

如果启用成功,就可以关注你的微信平台,输入内容,看看提示是不是welcome to wechat world!或者hello word!

以上全部内容就是针对微信公众平台做的讲解,希望可以帮助到大家。

延伸 · 阅读

精彩推荐