微信企业号发送监控消息【php】
公司这边有做监控异常并将消息发送到企业公众号的功能。大概如下:
定时任务检测异常消息 将消息存入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】的更多相关文章
- python与shell通过微信企业号发送消息
python与shell通过微信企业号发送信息,脚本来源于网络,做好搬运工,哈哈,相应的参考链接放在末位 shell版本: #!/bin/bash # CropID="xxxx" ...
- 微信企业号 发送信息 shell
微信企业号发送信息shell #可作为shell函数模块调用,用于微信通知.jenkins发版微信通知等等 # 微信API官方文档 https://work.weixin.qq.com/api/doc ...
- 通过微信企业号发送zabbix报警
采用微信报警时,管理员账户中必须要设置"示警媒体"一项,"收件人"一项可随便填写一下.其它成员则可以不用添加设置. ---------------------- ...
- JAVA微信公众号网页开发 —— 接收微信服务器发送的消息
WeixinMessage.java package com.test; import java.io.Serializable; /** * This is an object that conta ...
- 【原创】在 ASP.NET Core 3.1 中使用 Senparc.Weixin.Work 企业微信 SDK —— 发送文本消息
下面在 Web 空应用里展示一个简单的例子来实现发送文本消息. 本文目录: 创建 Web 空应用 命令行方式创建 添加SDK引用 命令行方式 进入项目目录 添加包引用 配置和使用SDK 添加appse ...
- 【原创】在 .NET Core 3.1 中使用 Senparc.Weixin.Work 企业微信 SDK —— 发送文本消息
下面在控制台应用里展示一个简单的例子来实现发送文本消息. 本文目录: 创建控制台应用 添加SDK引用 命令行方式 进入项目目录 添加包引用 配置和使用SDK 添加appsettings.json文件 ...
- [实例]JAVA调用微信接口发送图文消息,不用跳到详情页
package com.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputStrea ...
- django 微信企业号 返回text消息
from django.template import Context,Template textTemplate=""" <xml> <ToUserN ...
- 微信程序开发系列教程(三)使用微信API给微信用户发文本消息
这个系列的第二篇教程,介绍的实际是被动方式给微信用户发文本消息,即微信用户关注您的公众号时,微信平台将这个关注事件通过一个HTTP post发送到您的微信消息服务器上.您对这个post请求做了应答(格 ...
随机推荐
- Visual Basic 函数速查
Calendar 常数 可在代码中的任何地方用下列常数代替实际值: 常数 值 描述 vbCalGreg 0 指出使用的是阳历. vbCalHijri 1 指出使用的是伊斯兰历法. Color 常数 可 ...
- linux vim操作技巧
安装: NERDTree 从 http://www.vim.org/scripts/script.php?script_id=1658 下载 unzip NERD_tree.zip cd ~/.vi ...
- 实例: Java代码操作oracle数据库(JDBC+sevrlet+jsp+html)
1, 注册页面 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.or ...
- java集合:常用集合的数据结构
List 有序可重复 ArrayList ArrayList数据结构是数组.查询快,增删慢.ArrayList是线程不安全的,允许元素为null . Vector 线程安全的数组,效率较差,已经过时不 ...
- react-native react-navigation的用法
react-navigation升级了, 看这个: react-navigation 3.x版本的使用 一.问题背景 react-navigation是react-native官方推荐的,基于Java ...
- 删除链表中的元素 · Remove Linked List Elements
[抄题]: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
- Redis开发、管理实战
一.数据类型 String : 字符类型 Hash: 字典类型 List: 列表 Set: 集合 Sorted set: 有序集合 二.全局Key操作 KEYS * 查看KEY支持通配符 DEL 删除 ...
- [Selenium]怎样验证页面是否有无变化
验证方法:将两次的Dom结构进行对比 String beforeStr = (String) SeleniumUtil.getInnerHTML(page.getDriver(), page.getD ...
- Greeplum 系列(一) Greenplum 架构
Greeplum 系列(一) Greenplum 架构 Greenplum 可进行海量并行处理 (Massively Parallel Processing) 一.Greenplum 体系架构 Gre ...
- windows-x64 php5.6+apache2.4+mysql配置
随手一记, 方便以后查找! 1.安装apache2.4 - 下载压缩文件并解压到 D:\Develop\Apache24 - 修改 conf 目录下: httpd.conf 文件 - 服务器目录: ...