之前想用微信做监控,没有找到api,用公众号不会有信息提示。后来研究了一下企业微信,接口比较简单,几乎和钉钉一毛一样。同时微信和企业微信关联,可以产生信息提醒,不需要安装企业微信客户端。可以自己注册一个企业号,很方便。
使用方式:脚本名称 参数1 参数2
参数1为接受人员 ,多个用“|”分割;
参数2为信息内容
corpid,corpsecret,agentid根据自己注册的内容修改,脚本里边一部分查询部门列表的内容没有用可以删掉,token可以临时缓存一下提高效率。
#!/bin/php
<?php
//获取token
$corpid="ccccccccccccccccccccccccccccccccccccccccccc";
$corpsecret="ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
$agentid="ccccccc";
$userlist=array();
$url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$corpid."&corpsecret=".$corpsecret;
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
$output=curl_exec($ch);
curl_close($ch);
$out_array=json_decode($output,true);
$access_token=$out_array["access_token"];
echo $access_token;
$url="https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=".$access_token;
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
$output=curl_exec($ch);
curl_close($ch);
$out_array=json_decode($output,true);
print_r($out_array);
$url="https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=".$access_token."&department_id=1";
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
$output=curl_exec($ch);
curl_close($ch);
$out_array=json_decode($output,true);
print_r($out_array);
$url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".$access_token;
$post_array=array("touser" => $argv[1],"agentid"=> $agentid, "msgtype" => "text", "text" => array("content" => $argv[2]));
$post_string=json_encode($post_array);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($post_string))
);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
?>