公司这边有做监控异常并将消息发送到企业公众号的功能。大概如下:

 定时任务检测异常消息

 将消息存入redis队列

 定时处理队列异常消息

 发送到微信企业号对应的部门组

这里我们来看一下微信发送过程,其他不做讨论。

简单的来说,只需要两个步骤即可:

 获取AccessToken
发送消息到对应的项目部门组

一 获取AccessToken

AccessToken是企业号的全局唯一票据,调用接口时需携带AccessToken。
AccessToken需要用CorpID和Secret来换取,不同的Secret会返回不同的AccessToken。正常情况下AccessToken有效期为7200秒,有效期内重复获取返回相同结果。access_token至少保留512字节的存储空间。

请求说明

Https请求方式: GET
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=id&corpsecret=secrect corpid     企业Id
corpsecret  管理组的凭证密钥

二 发送消息

企业可以主动发消息给成员

请求说明

Https请求方式: POST
https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN
消息型应用支持文本、图片、语音、视频、文件、图文等消息类型。主页型应用只支持文本消息类型,且文本长度不超过20个字。

参数

  必须 说明
touser 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
toparty 部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
totag 标签ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
msgtype 消息类型,此时固定为:text (支持消息型应用跟主页型应用)
agentid 企业应用的id,整型。可在应用的设置页面查看
content 消息内容,最长不超过2048个字节,注意:主页型应用推送的文本消息在微信端最多只显示20个字(包含中英文)
safe 表示是否是保密消息,0表示否,1表示是,默认0

其实过程挺简单, 简单代码如下:

<?php
/**
* 微信公众号信息处理
*/
class WeixinMessage { //corpid
public $corpid = 'xxxxxx';
//sercret
public $corpsecret = 'xxxxx'; //微信发消息api
public $weixinSendApi = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='; /**
* 请求微信Api,获取AccessToken
*/
public function getAccessToken()
{
error_reporting(E_ALL);
//临时存放 并不安全
$filePath = ROOT.'cache/weixinToken.txt';
$tokenInfo = array();
if(is_file($filePath)){
$tokenInfo = json_decode(file_get_contents($filePath),TRUE);
}
if(!isset($tokenInfo['access_token']) || time()>$tokenInfo['expires_in']){
//更新access_token
$getAccessTokenApi = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$this->corpid}&corpsecret={$this->corpsecret}"; $jsonString = $this->curlGet($getAccessTokenApi);
$jsonInfo = json_decode($jsonString,true);
if(isset($jsonInfo['access_token'])) {
$jsonInfo['expires_in'] = time() + ;
file_put_contents($filePath, json_encode($jsonInfo));
}
$tokenInfo = $jsonInfo;
} if(isset($tokenInfo['access_token']) && $tokenInfo['expires_in']>time()){
return $tokenInfo['access_token'];
} else {
return FALSE;
}
} /**
* 发信息接口
*
* @author wanghan
* @param $content 发送内容
* @param $touser 接收的用户 @all全部 多个用 | 隔开
* @param $toparty 接收的群组 @all全部 多个用 | 隔开
* @param $totag 标签组 @all全部 多个用 | 隔开
* @param $agentid 应用id
* @param $msgtype 信息类型 text=简单文本
*/
public function send($content,$touser='@all',$toparty='',$totag='',$agentid=,$msgtype='text')
{
$api = $this->weixinSendApi.$this->getAccessToken();
$postData = array(
'touser' => $touser,
'toparty' => $toparty,
'totag' => $totag,
'msgtype' => $msgtype,
'agentid' => $agentid,
'text' => array(
'content' => urlencode($content)
)
); $postString = urldecode(json_encode($postData));
$ret = $this->curlPost($api,$postString);
$retArr = json_decode($ret,TRUE);
if(isset($retArr['errcode']) && $retArr['errcode'] == ) {
return true;
} else {
return false;
}
} /**
* Curl Post数据
* @param string $url 接收数据的api
* @param string $vars 提交的数据
* @param int $second 要求程序必须在$second秒内完成,负责到$second秒后放到后台执行
* @return string or boolean 成功且对方有返回值则返回
*/
function curlPost($url, $vars, $second=)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, );
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, );
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// 'Content-Type: application/json; charset=utf-8',
// 'Content-Length: ' . strlen($vars))
// );
$data = curl_exec($ch);
curl_close($ch);
if($data)
return $data;
return false;
} /**
* CURL get方式提交数据
* 通过curl的get方式提交获取api数据
* @param string $url api地址
* @param int $second 超时时间,单位为秒
* @param string $log_path 日志存放路径,如果没有就不保存日志,还有存放路径要有读写权限
* @return true or false
*/
function curlGet($url,$second=,$log_path='', $host='', $port='')
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
if(!empty($host)){
curl_setopt($ch,CURLOPT_HTTPHEADER,$host);
}
if(!empty($port)){
curl_setopt($ch,CURLOPT_PORT,$port);
}
$data = curl_exec($ch);
$return_ch = curl_errno($ch);
curl_close($ch);
if($return_ch!=)
{
if(!empty($log_path))
file_put_contents($log_path,curl_error($ch)."\n\r\n\r",FILE_APPEND);
return false;
}
else
{
return $data;
}
}
}

微信企业号发送监控消息【php】的更多相关文章

  1. python与shell通过微信企业号发送消息

    python与shell通过微信企业号发送信息,脚本来源于网络,做好搬运工,哈哈,相应的参考链接放在末位 shell版本: #!/bin/bash # CropID="xxxx" ...

  2. 微信企业号 发送信息 shell

    微信企业号发送信息shell #可作为shell函数模块调用,用于微信通知.jenkins发版微信通知等等 # 微信API官方文档 https://work.weixin.qq.com/api/doc ...

  3. 通过微信企业号发送zabbix报警

    采用微信报警时,管理员账户中必须要设置"示警媒体"一项,"收件人"一项可随便填写一下.其它成员则可以不用添加设置. ---------------------- ...

  4. JAVA微信公众号网页开发 —— 接收微信服务器发送的消息

    WeixinMessage.java package com.test; import java.io.Serializable; /** * This is an object that conta ...

  5. 【原创】在 ASP.NET Core 3.1 中使用 Senparc.Weixin.Work 企业微信 SDK —— 发送文本消息

    下面在 Web 空应用里展示一个简单的例子来实现发送文本消息. 本文目录: 创建 Web 空应用 命令行方式创建 添加SDK引用 命令行方式 进入项目目录 添加包引用 配置和使用SDK 添加appse ...

  6. 【原创】在 .NET Core 3.1 中使用 Senparc.Weixin.Work 企业微信 SDK —— 发送文本消息

    下面在控制台应用里展示一个简单的例子来实现发送文本消息. 本文目录: 创建控制台应用 添加SDK引用 命令行方式 进入项目目录 添加包引用 配置和使用SDK 添加appsettings.json文件 ...

  7. [实例]JAVA调用微信接口发送图文消息,不用跳到详情页

    package com.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputStrea ...

  8. django 微信企业号 返回text消息

    from django.template import Context,Template textTemplate=""" <xml> <ToUserN ...

  9. 微信程序开发系列教程(三)使用微信API给微信用户发文本消息

    这个系列的第二篇教程,介绍的实际是被动方式给微信用户发文本消息,即微信用户关注您的公众号时,微信平台将这个关注事件通过一个HTTP post发送到您的微信消息服务器上.您对这个post请求做了应答(格 ...

随机推荐

  1. JPA with Hibernate implementation

    https://code.google.com/p/jpa-basics-tutorial/source/checkout http://alextretyakov.blogspot.com/2013 ...

  2. 初步认识session

    TestSession01.java protected void doPost(HttpServletRequest request, HttpServletResponse response) t ...

  3. class.__subclasses__()

    [class.__subclasses__()] Each class keeps a list of weak references to its immediate subclasses. Thi ...

  4. ORACLE BI Publisher Enterprise

    二.带参数 BEGIN:{$FIRST_DAY_OF_MONTH()$} END:{$SYSDATE()$} 三\加下拉菜单值

  5. C++实现:把一个文件夹里的冗余文件(.txt)删除

    代码很简单,调用了MFC里的几个函数.这里的冗余判断,是要遍历文件内容,进行两两比较. 需要注意的地方有两点: 1.源文件里头文件<afx.h>必须放在最前面.这里是为了避免nafxcwd ...

  6. 27.Remove Element(Array)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  7. cmake 查看配置选项

    cmake 查看配置选项可以用如下命令 cmake . -LH 查看help > cmake -h    cmake version 2.6-patch 4 Usage cmake [optio ...

  8. 全球数据库-->基金/管理产品-->分类/行业平均

    ETF分类 GIFS台湾注册基金 GIFS开放式分类 GIFS德国注册基金 GIFS意大利注册基金 GIFS新兴市场 GIFS新加坡保险连结 GIFS新加坡注册基金 GIFS日本 GIFS比利时注册基 ...

  9. Oracle学习笔记(十三)

    十四.触发器(监听数据操作的工具) 1.什么是触发器? 数据库触发器是一个与表相关联的.存储的PL/SQL程序 作用: 每当一个特定的数据操作语句(insert.update.delete)在指定的表 ...

  10. 20169202 2016-2017-2《Windows攻击》

    Windows攻击 实验要求:使用Metaspoit攻击MS08-067,提交正确得到远程Shell的截图,加上自己的学号水印 (1):MS08-067远程溢出漏洞描述 MS08-067漏洞的全称为& ...