自建接口实现微信通知推送,以下演示Typecho平台下的微信通知推送,先看效果图:

一、建立推送API平台

1、下载代码到服务器

https://github.com/evrstr/wx-push

2、解压编辑代码

说明:index.php 里的配置

$corpid = "xxxx";
$secret = "xxxx";
$agentid = 100000x;

3、配置说明

关于这几个参数的获取可以参考文章:

自行配置 db.db 禁止外网访问。eg:nginx 可添加配置为:

location /db.db
{
    deny all;
}

4、接口说明

比如在wxpush文件内的接口为:http://127.0.0.1/wxpush/

GET 请求

type: 消息类型 textcard/text
title: 标题
msg: 消息
detail: 卡片消息详情界面的 html 代码字符串(使用此字段请尽量用 post 请求),这里主要是自定义模板,使详情页面更美观。

5、测试接口

http://127.0.0.1/wxpush/?type=textcard&msg=123&title=推送&detail=<p%20align="center"%20style="color:red;">测试html</p>

如果在微信里收到消息,则说明配置成功。

注意:php>=7.4则会出现错误 Deprecated: Function get_magic_quotes_gpc() is deprecated ,但不必理会

6、注意事项

如果你的API接口不是在根目录新建的,比如在wxpush等文件夹建立的,那么你需要修改index.php第68行代码url参数为

http://{$_SERVER['HTTP_HOST']}{$_SERVER["REQUEST_URI"]}/detail.php?id={$dbid}

二、Typecho插件

1、新建插件

新建目录usr/plugins/WxPush,新建文件usr/plugins/WxPush/Plugin.php,加入如下内容:

<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
 * 企业微信推送
 * 
 * @package WxPush 
 * @author 御驾亲征
 * @version 1.0.0
 * @link http://yjqz.cc
 */
class WxPush_Plugin implements Typecho_Plugin_Interface
{
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     * 
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate()
    {
        Typecho_Plugin::factory('Widget_Feedback')->finishComment = array(__CLASS__, 'send');
    }
    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     * 
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate()
    {
    }
    /**
     * 获取插件配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form)
    {
        $enable = new Typecho_Widget_Helper_Form_Element_Radio(
            'enable',
            array('off' => '关闭', 'on' => '开启(默认)'),
            'on',
            '是否开启',
            null
        );
        $form->addInput($enable);
        $type = new Typecho_Widget_Helper_Form_Element_Radio('type', array('textcard' => '卡片', 'text' => '文字'), 'textcard', _t("消息类型"));
        $form->addInput($type);
        $url = new Typecho_Widget_Helper_Form_Element_Text('url', NULL, 'https://接口/(请不要加index.php)', _t('推送地址'));
        $form->addInput($url->addRule('required', _t('您必须填写一个正确的 URL 地址')));
        $btn = new Typecho_Widget_Helper_Layout('button', array('type' => 'button', 'name' => 'btn', 'value' => 'button', 'onclick' => "var httpRequest = new XMLHttpRequest();
        httpRequest.open('GET', '" . $url->value . "?type=textcard&msg=123&title=推送&detail=<p>测试html</p>', true);
        httpRequest.send();"));
        $btn->html('测试');
        $form->addItem($btn);
    }
    /**
     * 个人用户的配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form)
    {
    }
    /**
     * 插件实现方法
     * 
     * @access public
     * @return void
     */
    public static function send($comment)
    {
        $options = Typecho_Widget::widget('Widget_Options');
        $enable = $options->plugin('WxPush')->enable;
        if ($enable == 'off') {
            return;
        }
        $url = $options->plugin('WxPush')->url;
        $type = $options->plugin('WxPush')->type;
        $title = '您的文章【' . $comment->title . "】收到一条新的评论!";
        $currentdata = date("Y-m-d H:i", time());
        $msg = "
        <div class=\"gray\">{$currentdata}</div>
        <div class=\"normal\">评论人:{$comment->author}</div>
        <div class=\"highlight\">评论内容:{$comment->text}</div>
        ";
        $detail = "
        <div style=\"width:99.8%;height:99.8%\">
        <tbody>
            <tr>
                <td
                    style=\"background:#fafafa url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAy0lEQVQY0x2PsQtAYBDFP1keKZfBKIqNycCERUkMKLuSgZnRarIpJX8s3zfcDe9+794du+8bRVHQOI4wDAOmaULTNDDGYFkWMVVVQUTQdZ3iOMZxHCjLElVV0TRNYHVdC7ptW6RpSn3f4wdJkiTs+w6WJAl4DcOAbdugKAq974umaRAEARgXn+cRW3zfFxuiKCJZloXGHMeBbdv4Beq6Duu6Issy7iYB8Jbnucg8zxPLsggnj/zvIxaGIXmeB9d1wSE+nOeZf4HruvABUtou5ypjMF4AAAAASUVORK5CYII=)\">
                    <div
                        style=\"border-radius:10px;font-size:13px;color:#555;width:666px;font-family:'Century Gothic','Trebuchet MS','Hiragino Sans GB','微软雅黑','Microsoft Yahei',Tahoma,Helvetica,Arial,SimSun,sans-serif;margin:50px auto;border:1px solid #eee;max-width:100%;background:#fff repeating-linear-gradient(-45deg,#fff,#fff 1.125rem,transparent 1.125rem,transparent 2.25rem);box-shadow:0 1px 5px rgba(0,0,0,.15)\">
                        <div
                            style=\"width:100%;background:#49BDAD;color:#fff;border-radius:10px 10px 0 0;background-image:-moz-linear-gradient(0deg,#DAD299,#B0DAB9);background-image:-webkit-linear-gradient(0deg,#DAD299,#B0DAB9);height:66px\">
                            <p
                                style=\"font-size:15px;word-break:break-all;padding:23px 32px;margin:0;background-color:hsla(0,0%,100%,.4);border-radius:10px 10px 0 0\">
                                您在 《<a href=\"{$comment->permalink}\" style=\"text-decoration:none;color:#fff\"
                                    target=\"_blank\">{$comment->title}</a>》 发表的文章有新评论!</p>
                        </div>
                        <div style=\"margin:40px auto;width:90%\">
                            <p>{$comment->author} 在您的《<a href=\"{$comment->permalink}\" style=\"text-decoration:none;color:#12addb\"
                                    target=\"_blank\">{$comment->title}</a>》上发表评论:</p>
                            <p
                                style=\"background:#fafafa repeating-linear-gradient(-45deg,#fff,#fff 1.125rem,transparent 1.125rem,transparent 2.25rem);box-shadow:0 2px 5px rgba(0,0,0,.15);margin:20px 0;padding:15px;border-radius:5px;font-size:14px;color:#555\">
                                {$comment->text}</p>
                            <p>请注意:此通知由 <a href=\"{$options->siteUrl}\" style=\"color:#12addb\" target=\"_blank\">{$options->title}</a>
                                自动发送,请勿直接回复。</p>
                                <p>若此通知不是您请求的,请忽略并删除!</p>
                                <p>{$currentdata}<p>
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
    </div>
";
        $data = [
            'type' => $type,
            'title' => $title,
            'msg' => $msg,
            'detail' => $detail
        ];
        $postdata = http_build_query($data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded;',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间(单位:s)
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
}

2、激活配置插件

注意接口填写:https://接口/(请不要加index.php)

测试按钮用的是GET请求,基本上没用,还是要评论测试效果(POST请求)

测试成功后享受插件带来的便利吧!

三、参考资料

1、【开源】搭建微信消息推送服务

2、【插件】typecho评论微信通知插件

本文地址:https://www.jishusongshu.com/code-basic/typecho-comment-wechat-pusher







文件名称:wx-push-master
适用版本:PHP
更新日期:2021-09-20
文件大小:5 KB

点击下载

Invitation
QQ Group
1095632335

created:04/01/2020

Welcome to the Group

Use this card to join us and participate in a pleasant discussion together .

Welcome to JISHUSONGSHU Group,wish you a nice day .