当前位置:首页 > 技术分享

PHP微信模板消息推送服务 开发的微信测试公众号模板消息推送服务 极简且免费 测试公众号每天有10万次额度 不会技术也能玩推送

admin3周前 (01-23)技术分享67

一个基于PHP开发的微信模板消息推送服务支持PHP5.6+版本。开源地址:https://gitee.com/web/php-wxpush

功能特性

  • ✅ 支持GET和POST请求方式

  • ✅ 支持JSON和表单参数解析

  • ✅ 微信AccessToken自动获取与缓存

  • ✅ 支持多appid独立缓存

  • ✅ 自动处理AccessToken过期

  • ✅ 消息详情页面展示

  • ✅ 动态粒子背景效果

  • ✅ 响应式设计,适配各种设备

  • ✅ 兼容PHP5.6+版本

  • ✅ 支持自定义时区

  • ✅ 完整的错误处理机制

⚠️ 部署条件

  • 微信公众平台接口测试帐号申请 

  • wx1.png

  • 获取appid 、appsecret 

    wx2.png

  • 关注测试公众号,获取userid(微信号),新增测试模板(注意模版内容填写格式 内容: {{content.DATA}}) 获取template_id(模板ID) 

    wx3.png

  • 将以上获取到的参数代入下面使用即可 (我的手机出不来,下面的弹框只能到公众号里才能看到。可能和手机有关系,也可能是微信改版了)

    w0.jpg w1.jpg

体验接口权限

接口官方文档每日调用上限/次
获取access_token查看2000
模板消息(业务通知)查看100000

安装部署

环境要求

  • PHP 5.6 或更高版本

  • cURL 扩展

  • JSON 扩展

部署步骤

  1. 下载项目文件

    git clone https://gitee.com/web/php-wxpush.gitcd php-wxpush
  2. 配置默认参数和API密钥

    • 编辑 config.php 文件,配置默认参数和API密钥

    • 可以根据不同appid配置不同的默认参数

    • 可以配置API密钥,增强API调用安全性

    • 示例配置:

    return [
        // 默认模板配置
        'default' => [
            'template_id' => 'your_template_id',
            'title' => '默认标题',
            'content' => '默认内容',
            'base_url' => 'https://your-domain.com',
            'tz' => 'Asia/Shanghai',
        ],
        
        // API密钥配置(可选,用于增强API安全性)
        // 默认为空数组,不启用API密钥验证
        // 配置后,调用/wxsend接口必须传递有效的apikey参数
        'apikeys' => [
            'your_api_key_1',
            'your_api_key_2',
        ],
        
        // 按appid配置不同的默认参数
        'appids' => [
            'your_appid_here' => [
                'template_id' => 'your_template_id',
                'title' => '应用专属标题',
                'content' => '应用专属内容',
                'base_url' => 'https://your-domain.com',
                'tz' => 'Asia/Shanghai',
            ],
        ],
        
        // 系统配置
        'system' => [
            'cache_dir' => './cache/',
            'timezone' => 'Asia/Shanghai',
        ],
    ];
  3. 部署到Web服务器

    • 将项目文件上传到Web服务器的根目录或子目录

    • 确保Web服务器配置正确,能够解析PHP文件

    • 确保缓存目录有写入权限

  4. 配置Web服务器

    • Apache:确保.htaccess文件配置正确(可选)

    • Nginx:示例配置

      server {
          listen 80;
          server_name your-domain.com;
          root /path/to/php-wxpush;
          index index.php;
          
          location / {
              try_files $uri $uri/ /index.php$is_args$args;
          }
          
          location ~ \.php$ {
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }
      }
  5. 测试域名(仅供测试,请勿用于生效环境)

https://wxpush.bitefu.net/wxsend

使用方法

1. 通过HTTP请求使用

发送微信模板消息

GET请求(完整参数)

http://your-domain/index.php/wxsend?appid=your_appid&secret=your_secret&userid=your_userid&template_id=your_template_id&title=测试标题&content=测试内容

GET请求(使用默认参数)

http://your-domain/index.php/wxsend?appid=your_appid&secret=your_secret&userid=your_userid

GET请求(使用API密钥)

http://your-domain/index.php/wxsend?appid=your_appid&secret=your_secret&userid=your_userid&template_id=your_template_id&apikey=your_api_key

POST请求(JSON,完整参数)

curl -X POST -H "Content-Type: application/json" -d '{
    "appid": "your_appid",
    "secret": "your_secret",
    "userid": "your_userid",
    "template_id": "your_template_id",
    "title": "测试标题",
    "content": "测试内容"
}' http://your-domain/index.php/wxsend

POST请求(JSON,使用默认参数)

curl -X POST -H "Content-Type: application/json" -d '{
    "appid": "your_appid",
    "secret": "your_secret",
    "userid": "your_userid"
}' http://your-domain/index.php/wxsend

POST请求(JSON,使用API密钥)

curl -X POST -H "Content-Type: application/json" -d '{
    "appid": "your_appid",
    "secret": "your_secret",
    "userid": "your_userid",
    "template_id": "your_template_id",
    "apikey": "your_api_key"
}' http://your-domain/index.php/wxsend

POST请求(表单,完整参数)

curl -X POST -d "appid=your_appid&secret=your_secret&userid=your_userid&template_id=your_template_id&title=测试标题&content=测试内容" http://your-domain/index.php/wxsend

POST请求(表单,使用默认参数)

curl -X POST -d "appid=your_appid&secret=your_secret&userid=your_userid" http://your-domain/index.php/wxsend

POST请求(表单,使用API密钥)

curl -X POST -d "appid=your_appid&secret=your_secret&userid=your_userid&template_id=your_template_id&apikey=your_api_key" http://your-domain/index.php/wxsend
ThinkPHP伪静态模式

GET请求(使用s参数)

http://your-domain/index.php?s=/wxsend&appid=your_appid&secret=your_secret&userid=your_userid

GET请求(使用s参数和API密钥)

http://your-domain/index.php?s=/wxsend&appid=your_appid&secret=your_secret&userid=your_userid&template_id=your_template_id&apikey=your_api_key

POST请求(JSON,使用s参数)

curl -X POST -H "Content-Type: application/json" -d '{"appid":"your_appid","secret":"your_secret","userid":"your_userid"}' "http://your-domain/index.php?s=/wxsend"

POST请求(JSON,使用s参数和API密钥)

curl -X POST -H "Content-Type: application/json" -d '{"appid":"your_appid","secret":"your_secret","userid":"your_userid","template_id":"your_template_id","apikey":"your_api_key"}' "http://your-domain/index.php?s=/wxsend"

POST请求(表单,使用s参数)

curl -X POST -d "appid=your_appid&secret=your_secret&userid=your_userid" "http://your-domain/index.php?s=/wxsend"

POST请求(表单,使用s参数和API密钥)

curl -X POST -d "appid=your_appid&secret=your_secret&userid=your_userid&template_id=your_template_id&apikey=your_api_key" "http://your-domain/index.php?s=/wxsend"

查看消息详情

标准模式

http://your-domain/index.php/detail?title=测试标题&message=测试内容&date=2023-01-01%2012:00:00

ThinkPHP伪静态模式

http://your-domain/index.php?s=/detail&title=测试标题&message=测试内容&date=2023-01-01%2012:00:00

msg.png

2. 作为PHP类使用

// 引入微信推送类
require_once 'wxpush.php';
// 实例化微信推送类
$wxPush = new WxPush();
// 处理请求
$wxPush->handleRequest();

API文档

1. 发送微信模板消息

接口地址/wxsend

请求方式:GET/POST

请求参数

参数名类型必选描述默认值
appidstring微信AppID
secretstring微信AppSecret
useridstring接收消息的用户openid
template_idstring模板消息ID
apikeystringAPI密钥,当config.php中配置了apikeys时必填
titlestring消息标题测试标题
contentstring消息内容测试内容
base_urlstring详情页基础URL当前域名
tzstring时区Asia/Shanghai
template_dataarray完整的模板数据数组,格式:{"keyword1":{"value":"值1"},"keyword2":{"value":"值2"}}
template_paramsarray模板参数数组,格式:{"keyword1":{"value":"值1"},"keyword2":{"value":"值2"}}
template_*mixed单个模板参数,如template_keyword1=值1,会转换为{"keyword1":{"value":"值1"}}

返回结果

{    "errcode": 0,    "errmsg": "ok"}

2. 查看消息详情

接口地址/detail

请求方式:GET

请求参数

参数名类型必选描述默认值
titlestring消息标题消息推送
messagestring消息内容无告警信息
datestring发送时间无时间信息

返回结果:HTML页面

3. 服务状态

接口地址/

请求方式:GET

返回结果

{    "message": "php-wxpush is running..."}

示例代码

1. 直接调用类文件示例

完整参数示例

<?php
// 直接调用WxPush类发送消息
require_once 'wxpush.php';

// 初始化微信推送类
$wxPush = new WxPush();

// 准备发送参数
$params = [
    'appid' => 'your_appid',
    'secret' => 'your_secret',
    'userid' => 'your_userid',
    'template_id' => 'your_template_id',
    'title' => '测试标题',
    'content' => '测试内容',
    'base_url' => 'https://your-domain.com', // 可选,默认使用当前域名
    'tz' => 'Asia/Shanghai' // 可选,默认时区
];

try {
    // 直接调用public方法发送消息(推荐方式)
    $result = $wxPush->sendMessage($params);
    
    echo "发送结果:\n";
    print_r($result);
} catch (Exception $e) {
    echo "发送失败:" . $e->getMessage() . "\n";
}


使用自定义模板参数示例

<?php
// 直接调用WxPush类发送消息(使用自定义模板参数)
require_once 'wxpush.php';

// 初始化微信推送类
$wxPush = new WxPush();

// 方式1:使用template_data参数(完整的模板数据)
$params1 = [
    'appid' => 'your_appid',
    'secret' => 'your_secret',
    'userid' => 'your_userid',
    'template_id' => 'your_template_id',
    'template_data' => [
        'keyword1' => ['value' => '订单号:123456'],
        'keyword2' => ['value' => '支付金额:100元'],
        'keyword3' => ['value' => '支付时间:2023-01-01 12:00:00'],
        'keyword4' => ['value' => '支付状态:成功']
    ]
];

// 方式2:使用template_params参数(数组形式的模板参数)
$params2 = [
    'appid' => 'your_appid',
    'secret' => 'your_secret',
    'userid' => 'your_userid',
    'template_id' => 'your_template_id',
    'template_params' => [
        'keyword1' => ['value' => '订单号:123456'],
        'keyword2' => ['value' => '支付金额:100元']
    ]
];

// 方式3:使用单个template_*参数
$params3 = [
    'appid' => 'your_appid',
    'secret' => 'your_secret',
    'userid' => 'your_userid',
    'template_id' => 'your_template_id',
    'title' => '订单通知',
    'content' => '您的订单已支付成功',
    'template_keyword1' => '订单号:123456',
    'template_keyword2' => '支付金额:100元',
    'template_keyword3' => ['value' => '支付时间:2023-01-01 12:00:00', 'color' => '#FF0000']
];

try {
    // 使用方式1发送消息
    $result = $wxPush->sendMessage($params1);
    
    echo "发送结果:\n";
    print_r($result);
} catch (Exception $e) {
    echo "发送失败:" . $e->getMessage() . "\n";
}


使用API密钥示例

<?php
// 直接调用WxPush类发送消息(使用API密钥)
require_once 'wxpush.php';

// 初始化微信推送类
$wxPush = new WxPush();

// 当config.php中配置了apikeys时,必须传递apikey参数
$params = [
    'appid' => 'your_appid',
    'secret' => 'your_secret',
    'userid' => 'your_userid',
    'template_id' => 'your_template_id',
    'apikey' => 'your_api_key', // API密钥,与config.php中配置的apikeys数组中的某个值匹配
    'title' => '测试标题',
    'content' => '测试内容'
];

try {
    // 发送消息
    $result = $wxPush->sendMessage($params);
    
    echo "发送结果:\n";
    print_r($result);
} catch (Exception $e) {
    echo "发送失败:" . $e->getMessage() . "\n";
}


2. PHP HTTP请求示例

<?php
// 发送HTTP请求示例
function sendWechatMessage($params) {
    $url = 'http://your-domain/index.php/wxsend';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    return json_decode($response, true);
}

// 使用示例
$params = [
    'appid' => 'your_appid',
    'secret' => 'your_secret',
    'userid' => 'your_userid',
    'template_id' => 'your_template_id',
    'title' => '测试标题',
    'content' => '测试内容'
];

$result = sendWechatMessage($params);
print_r($result);


3. Python示例

import requests
import json

url = 'http://your-domain/index.php/wxsend'

params = {
    'appid': 'your_appid',
    'secret': 'your_secret',
    'userid': 'your_userid',
    'template_id': 'your_template_id',
    'title': '测试标题',
    'content': '测试内容'
}

response = requests.post(url, data=json.dumps(params), headers={'Content-Type': 'application/json'})
print(response.json())

4. Shell示例


# GET请求
curl "http://your-domain/index.php/wxsend?appid=your_appid&secret=your_secret&userid=your_userid&template_id=your_template_id&title=测试标题&content=测试内容"
# POST JSON请求
curl -X POST -H "Content-Type: application/json" -d '{"appid":"your_appid","secret":"your_secret","userid":"your_userid","template_id":"your_template_id","title":"测试标题","content":"测试内容"}' http://your-domain/index.php/wxsend


注意事项

  1. 微信模板消息配置

    • 需要在微信公众平台中配置好模板消息

    • 模板消息必须包含titlecontent字段

    • 确保模板ID与实际配置一致

  2. AccessToken缓存

    • AccessToken默认缓存7000秒(约1小时56分钟)

    • 缓存文件存储在./cache/目录下

    • 确保缓存目录有写入权限

    • 每个appid独立缓存,互不影响

  3. 错误处理

    • 接口返回的错误码与微信官方保持一致

    • 详细错误信息可查看服务器日志

  4. 安全性

    • 建议在生产环境中使用HTTPS协议

    • 不要将AppSecret泄露给第三方

    • 建议设置IP白名单限制访问

  5. 性能优化

    • 对于高并发场景,建议使用Redis等内存缓存替代文件缓存

    • 可以根据实际情况调整缓存过期时间

许可证

MIT License

更新日志

v1.0.0 (2026-01-23)

  • 初始版本

  • 实现微信模板消息推送功能

  • 支持GET和POST请求

  • 实现AccessToken缓存机制

  • 提供消息详情页面

  • 兼容PHP5.6+版本

贡献

欢迎提交Issue和Pull Request!

联系方式

如有问题或建议,请通过以下方式联系:

参考项目

本项目参考了以下优秀项目:

  • go-wxpush - Go语言实现的微信模板消息推送服务

感谢

感谢原项目作者的开源贡献,本项目是基于 go-wxpush 项目的PHP实现,保持了与原项目完全兼容的API功能。


PHP微信模板消息推送服务 - 简单、高效、可靠的微信消息推送解决方案!


扫描二维码推送至手机访问。

版权声明:本文由小刚刚技术博客发布,如需转载请注明出处。

本文链接:https://blog.bitefu.net/post/723.html

分享给朋友:

“PHP微信模板消息推送服务 开发的微信测试公众号模板消息推送服务 极简且免费 测试公众号每天有10万次额度 不会技术也能玩推送” 的相关文章

节假日api 开源版 解密

介绍免费节假日API 开源了,可以离线调用, 可以集成到自己系统中开源地址https://gitee.com/web/holidays_api开源说明注:原百度节假日API即为本人提供,后百度apistore禁止个人开发者所以才有此独立接口…

linux centos svn搭建及网站同步更新

以前上线的项目都是用ftp来更新的.后来技术人员多了,ftp的弊端就出来了.所以在这里给大家介绍一下我们现在正在使用的svn同步修改更新的方案.原理很简单主要是用到了svn的hooks功能.即本地提交到服务器的svn服务器.然后svn服务器…

centos 配置Let's Encrypt 泛域名https证书

centos 配置Let's Encrypt 泛域名https证书

前言2018年1月份Letsencrypt可以申请泛域名证书,这让我们部署多域名、多站点https省了很多功夫,终于可以不用维护多个域名的https证书。笔者以acme.sh为例,手把手教你配置https证书~本教程适用于centos 6.…

贾氏鸣天鼓健耳养肾操

贾氏鸣天鼓健耳养肾操

《贾氏鸣天鼓健耳养肾操》鸣天鼓是健耳强肾治耳病的古法,贾氏越云自创的鸣天鼓健耳养肾操是在古法的基础上创建。顺序:1静坐挺胸。2双手放心脏位置的胸口,左手掌盖住右手掌。3闭目静心,深呼吸19下。4双手相互搓揉,让手掌发热。5用发热的双手手掌严…

PHP和Redis实现在高并发下的抢购及秒杀功能

抢购、秒杀是平常很常见的场景,面试的时候面试官也经常会问到,比如问你淘宝中的抢购秒杀是怎么实现的等等。抢购、秒杀实现很简单,但是有些问题需要解决,主要针对两个问题:一、高并发对数据库产生的压力二、竞争状态下如何解决库存的正确减少("…

用CMD命令查询域名的DNS解析记录:A,NS,MX,CNAME,TXT

1、查询域名的A记录nslookup -qt=A bitefu.net当然查询A记录你直接用ping命令来ping域名也可以获得A记录。2、查询域名的NS记录nslookup -qt=NS bitefu.net3、查询域名的MX记录nslo…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。