企业微信实现发送应用消息推送
作者:
秒速五厘米
根据企业微信API请求流程可知,要想调用企业微信API,先获得access_token并缓存,因为每天的调用次数有限,access_token有效期为2小时,所以要进行缓存。然后再调用业务API,完成你请求的接口。
我这里用单页面实现整个过程(获取access_token,缓存access_token,发送推送)
<?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
);
// 图文消息体
$mpnews = array(
"touser" : "@all",
"toparty" : "PartyID1 | PartyID2",
"totag": "TagID1 | TagID2",
"msgtype" : "mpnews",
"agentid" : 1,
"mpnews" : {
"articles":[
{
"title": "Title",
"thumb_media_id": "MEDIA_ID",
"author": "Author",
"content_source_url": "URL",
"content": "Content",
"digest": "Digest description"
}
]
},
"safe":0,
"enable_id_trans": 0,
"enable_duplicate_check": 0,
"duplicate_check_interval": 1800
);
// 调用发送函数
echo send(json_encode($postdata));
?>上方的代码直接访问即可完成推送。
文本卡片消息体是发送文本卡片的数据,通过JSON格式数据进行发送给企业微信API,具体的参数说明请看开发文档:https://developer.work.weixin.qq.com/document/path/90236
// 文本卡片消息体 $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 );
touser为@all就是向所有人推送消息,msgtype就是当前发送的类型,agentid就是当前应用的id,textcard就是一些参数(标题、跳转的链接等)。
除了上面的消息类型,还有文本消息、图片消息、语音消息、视频消息、图文卡片消息等,具体可以查看开发文档,配置发送的消息体。
如果你微信关注了这个应用,那么就可以在微信收到通知,如过没有关注,就只能在企业微信内收到通知。
企业微信开发文档
https://developer.work.weixin.qq.com/document/path/90236