企业微信机器人推送demo php
之前项目里需要对接企业微信机器人推送接口,然后去微信开发平台找接口文档。发现并没有php相关的demo。 下面是代码,仅供参考。
class WechatBotService
{
protected $error = '';
protected $url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send";
protected $uploadUrl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media';
public function send()
{
$param = request()->param();
$type = $param['type'];
$token = $param['token'];
$msg = $param['msg'];
switch ($type)
{
case "text":
$where = ['token'=>$token,'status'=>1];
$group = new Group;
$key = $group->where($where)->field('url_key')->find()->toArray();
$this->sendText($key['url_key'],$msg);
break;
case "markdown":
$where = ['token'=>$token,'status'=>1];
$group = new Group;
$key = $group->where($where)->field('url_key')->find()->toArray();
$this->sendMarkDown($key['url_key'],str_replace(['\n','\\'],'',$msg));
break;
case "img":
$where = ['token'=>$token,'status'=>1];
$group = new Group;
$key = $group->where($where)->field('url_key')->find()->toArray();
$base64 = str_replace("data:image/jpeg;base64,","",$msg[0]);
$md5 = $msg[1];
$this->sendImg($key['url_key'],$base64,$md5);
break;
case "news":
$where = ['token'=>$token,'status'=>1];
$group = new Group;
$key = $group->where($where)->field('url_key')->find()->toArray();
$title = $_POST['title'];
$desc = $_POST['desc'];
$href = $_POST['href'];
$picUrl = $_POST['picUrl'];
$this->sendNews($key['url_key'],$title,$desc,$href,$picUrl);
break;
case "file":
$where = ['token'=>$token,'status'=>1];
$group = new Group;
$key = $group->where($where)->field('url_key')->find()->toArray();
$this->sendFile($key['url_key']);
break;
}
}
public function sendText($key, $msg)
{
if (empty($key) || !is_string($key))
{
$this->error = 'key为空';
return false;
}
if (empty($msg) || !is_string($msg))
{
$this->error = '发送内容为空';
return false;
}
$urls = $this->url.'?key='.$key;
$content = ['msgtype'=>'text','text'=>['content'=>$msg]];
$res = $this->sendRquest($urls,$content);
if (!isset($res['errcode']) || $res['errcode'] != '0') {
$this->error = '推送失败, errmsg: ' . $res['errmsg'];
return false;
}
return dump(json_encode($res));
}
public function sendMarkDown($key,$msg)
{
if (empty($key) || !is_string($key)) {
$this->error = 'key为空';
return false;
}
if (empty($msg) || !is_string($msg)) {
$this->error = '发送内容为空';
return false;
}
$urls = $this->url.'?key='.$key;
$content = ['msgtype'=>'markdown','markdown'=>['content'=>$msg]];
$res = $this->sendRquest($urls,$content);
if (!isset($res['errcode']) || $res['errcode'] != '0') {
$this->error = '推送失败, errmsg: ' . $res['errmsg'];
return false;
}
return dump(json_encode($res));
}
public function sendImg($key, $base64, $md5)
{
if (empty($key) || !is_string($key))
{
$this->error = 'key为空';
return false;
}
if (empty($md5) || !is_string($md5))
{
$this->error = '图片md5为空';
return false;
}
if (empty($base64) || !is_string($base64))
{
$this->error = '图片base64为空';
return false;
}
$urls = $this->url.'?key='.$key;
$content = ['msgtype'=>'image','image'=>['base64'=>$base64,'md5'=>$md5]];
$res = $this->sendRquest($urls,$content);
if (!isset($res['errcode']) || $res['errcode'] != '0') {
$this->error = '推送失败, errmsg: ' . $res['errmsg'];
return false;
}
return dump(json_encode($res));
}
public function sendNews($key, $title, $desc, $href, $picUrl)
{
if (empty($key) || !is_string($key))
{
$this->error = 'key为空';
return false;
}
if (empty($title) || !is_string($title))
{
$this->error = '标题不能为空';
return false;
}
if (empty($desc) || !is_string($desc))
{
$this->error = '描述内容不能为空';
return false;
}
if (empty($href) || !is_string($href))
{
$this->error = '跳转链接不能未空';
return false;
}
if (empty($picUrl) || !is_string($picUrl))
{
$this->error = '图片链接不能未空';
return false;
}
$urls = $this->url.'?key='.$key;
$data = [
'msgtype' => 'news',
'news' => [
'articles' => [
[
'title' => $title,
'description' => $desc,
'url' => $href,
'picurl' => $picUrl,
]
],
],
];
$res = $this->sendRquest($urls,$data);
if (!isset($res['errcode']) || $res['errcode'] != '0') {
$this->error = '推送失败, errmsg: ' . $res['errmsg'];
return false;
}
return dump(json_encode($res));
}
public function sendFile($key)
{
if (empty($key) || !is_string($key)) {
$this->error = 'key为空';
return false;
}
$url = $this->url . '?key=' . $key;
$up_url = $this->uploadUrl . '?key=' . $key . '&type=file';
$path = $this->uploadFile();
$cfile = new \CURLFile($path['path'], '', $path['name']);
$files = ['file' => $cfile];
$res = $this->sendRquest($up_url, $files, 'post', false);
$media_id = json_decode($res, true)['media_id'];
$data = ['msgtype' => 'file', 'file' => ['media_id' => $media_id]];
$res = $this->sendRquest($url, $data);
if (!isset($res['errcode']) || $res['errcode'] != '0') {
$this->error = '推送失败, errmsg: ' . $res['errmsg'];
return false;
}
return dump(json_encode($res));
}
public function uploadFile()
{
$filename = $_FILES['msg']['name'];
$temp_name = $_FILES['msg']['tmp_name'];
$size = $_FILES['msg']['size'];
$error = $_FILES['msg']['error'];
if ($size > 2*1024*1024*1024){
echo "<script>alert('文件大小超过2M大小');window.history.go(-1);</script>";
exit();
}
$arr = pathinfo($filename);
$url = ROOT_PATH . 'public' . DS . 'uploads\\';
$file_name = iconv('utf-8', 'gbk', $arr['basename']);
$ext_suffix = $arr['extension'];
$allow_suffix = array('txt','jpg','gif','jpeg','png','pdf','doc','xls','xlsx','ppt','csv');
if(!in_array($ext_suffix, $allow_suffix)){
echo "<script>alert('上传的文件类型只能是jpg,gif,jpeg,png');window.history.go(-1);</script>";
exit();
}
if (!file_exists($url)){
mkdir($url);
}
if (move_uploaded_file($temp_name, $url.$file_name)){
return ['path'=>$url.$file_name,'name'=>$file_name];
}else{
echo "<script>alert('文件上传失败,错误码:$error');</script>";
}
}
protected function sendRquest($url, $data = [], $type = 'post', $toJson = true, $timeOut = 0)
{
$ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, ($type == 'post') ? true : false);
if($toJson == true)
{
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
));
}else{
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($ssl) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if (!empty($timeOut)) {
curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
}
$html = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($html === false || $status != 200) {
var_dump(curl_error($ch), $status);
}
curl_close($ch);
$result = ($toJson) ? json_decode($html, true) : $html;
return $result;
}
}
|