/WxWork_Push

利用企业微信推送消息

Primary LanguagePHP

企业微信文本型信息

请求方式:GET/POST

认证方式:无

鉴权方式:无

url参数:

参数名 类型 内容 必要性 备注
msg str 发送的信息 不支持转义符
title str 标题
type str 消息类型 textcard:文本卡片(默认)
text:普通文本

示例:

now_time=`date "+%Y-%m-%d_%H:%M:%S"`
curl "http:/***/index.php?msg=${now_time}"

json回复:

根对象:

字段 类型 内容 备注
errcode num 返回码 0:成功
44004:msg为空
errmsg str 对返回码的文本描述内容 OK:成功
msgid str 消息id,用于撤回应用消息 发送成功才有

示例1:

{"errcode":0,"errmsg":"ok","msgid":"mrVtVXE39it1tWVvd57npPRRIMH-u_1oUfMvbyFJiH****************dXQ9Idv_02fD-0hbA"}

示例2:

{"errcode":44004,"errmsg":"empty content, hint: [16867233524*****499497495], from ip: 47.***.***.120, more info at https:\/\/open.work.weixin.qq.com\/devtool\/query?e=44004"}

开发文档

源码

<?php
//配置消息通知参数
//msg_type:消息类型(非必要)
//msg:内容
//msg_title:标题(非必要)
if (isset($_GET['type'])) {
    $msg_type = $_GET['type'];
} else {
	$msg_type = "textcard";
}
if (isset($_GET['title'])) {
    $msg_title = $_GET['title'];
} else {
	$msg_title = "Title";
}
if (isset($_GET['msg'])) {
    $content = $_GET['msg'];
}

//此处配置你的信息
$corpid = "";
$secret = "";
$agentid = 1;

//获取当前时间
$now_time=date("Y-m-d H:i:s");

//获取access_token
$url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' . $corpid . '&corpsecret=' . $secret;
$token = https_request($url);
$access_token = $token['access_token'];

if ($msg_type == "text") {
	// 文本格式
	$data = [
		'touser' => '@all',
		'msgtype' => $msg_type,
		'agentid' => $agentid,
		'text' => [
			'content' => $now_time . "\n" . $content
		]
	];
} else {
	// 文本卡片
	$data = [
		'touser' => '@all',
		'msgtype' => "textcard",
		'agentid' => $agentid,
		'textcard' => [
			'title' => $msg_title,
			"description" => "<div class=\"gray\">" . $now_time . "</div><div class=\"normal\">" . $content . "</div>",
            "url" => "None"
		],
		"enable_id_trans"=> 1,
		"enable_duplicate_check"=> 1,
		"duplicate_check_interval"=> 5
	];
}
$messageUrl = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' . $access_token;
$message = https_request($messageUrl, 'post', $data);
echo json_encode($message);

// CURL
function https_request($url, $format = 'get', $data = null)
{
    $headerArray = array("Content-type:application/json;", "Accept:application/json");
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if ($format == 'post') {
        curl_setopt($curl, CURLOPT_POST, 1);
        if ($data) {
            $data  = json_encode($data);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
    $data = json_decode(curl_exec($curl), true);
    // $data=curl_exec($curl);
    curl_close($curl);
    return $data;
}