文章目录
本文最后更新于 2022-10-14,已超过 365 天未更新,内容可能已失效。

wordpress实时推送微信

在开发这个功能前,主要是自己有点懒~~不想时时刻刻都去打开后台关注。目前zpro主题已经开发了如下的功能。企业微信或者微信能实时的接收到如下的推送。是不是很方便呢?

基本现有的代码都是基于评论推送

 

参考代码

<?php
 
// 声明页面header
header("Content-type:text/html;charset=utf-8");
 
// 获取access_token
function getToken(){
 
    // 定义id和secret
    $corpid='你的企业微信企业ID';
    $corpsecret='你的企业微信secret';
 
    // 读取access_token
    include './access_token.php';
 
    // 判断是否过期
    if (time() > $access_token['expires']){
 
        // 如果已经过期就得重新获取并缓存
        $access_token = array();
        $access_token['access_token'] = getNewToken($corpid,$corpsecret);
        $access_token['expires']=time()+7000;
         
        // 将数组写入php文件
        $arr = '<?php'.PHP_EOL.'$access_token = '.var_export($access_token,true).';'.PHP_EOL.'?>';
        $arrfile = fopen("./access_token.php","w");
        fwrite($arrfile,$arr);
        fclose($arrfile);
 
        // 返回当前的access_token
        return $access_token['access_token'];
 
    }else{
 
        // 如果没有过期就直接读取缓存文件
        return $access_token['access_token'];
    }
}
 
// 获取新的access_token
function getNewToken($corpid,$corpsecret){
    $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpid}&corpsecret={$corpsecret}";
    $access_token_Arr =  https_request($url);
    return $access_token_Arr['access_token'];
}
 
// curl请求函数
function https_request ($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $out = curl_exec($ch);
    curl_close($ch);
    return  json_decode($out,true);
}
 
// 发送应用消息函数
function send($data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.getToken());
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    return curl_exec($ch);
}
 
// 文本卡片消息体
$postdata = array(
    'touser' => '@all',
    'msgtype' => 'textcard',
    'agentid' => '1000002',
    'textcard' => array(
        'title' => '测试卡片的标题',
        'description' => '测试卡片的描述',
        'url' => 'http://www.qq.com',
        'btntxt' => '阅读全文',
    ),
    'enable_id_trans' => 0,
    'enable_duplicate_check' => 0,
    'duplicate_check_interval' => 1800
);
 
// 调用发送函数
echo send(json_encode($postdata));
?>

本源码来自 一个php文件实现企业微信推送通知,企业微信实现发送应用消息,推送通知到微信 – 『编程语言区』 – 吾爱破解 – LCG – LSG |安卓破解|病毒分析|www.52pojie.cn

操作步骤

wordpress推送微信 (qq.com)

CC 许可协议
署名-非商业性使用-相同方式共享 4.0 国际 了解协议
文章链接:https://zptheme.com/5370.html
本文作者:子佩·来源:子佩工作室

除非注明,子佩工作室 文章均为原创,转载请以链接形式标明本文地址。

相关推荐

登录后即可发表评论

社交账号登录

全部评论 (0)