5
头图

Summary

As we all know, the service account has a template message interface, which can actively push notification messages to users, but some time ago, the WeChat team began to notify developers that the template message was about to be adjusted to a subscription message, that is, the user accepts the subscription to receive the push. If there is no change in WeChat, we can receive notifications through enterprise WeChat.

Configuration steps

1. Register the company WeChat, and individuals can also register
2. After registration, log in to the enterprise WeChat to complete basic information
3. Click Application Management -> Self-build -> Create Application

image.png

4. After the creation is successful, simply modify your own information
5. Get the company ID and Secret

The company ID can be obtained in my company

image.png

App Secret can be viewed at the app you created

image.png

Development

According to the enterprise WeChat API request process, if you want to call the enterprise WeChat API, first obtain the access_token and cache it. Because the number of calls per day is limited and the access_token is valid for 2 hours, it needs to be cached. Then call the business API to complete the interface you requested.

Here I use a single page to implement the whole process (get access_token, cache access_token, send push)

<?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
);

// 调用发送函数
echo send(json_encode($postdata));
?>

The above code can be accessed directly to complete the push.

Description

The text card message body is the data to send the text card, which is sent to the enterprise WeChat API through JSON format data. For specific parameter descriptions, please see the development document: https://work.weixin.qq.com/api/doc/90000/ 90135/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 is @all to push messages to everyone, msgtype is the currently sent type, agentid is the id of the current application, and textcard is some parameters (title, jump link, etc.).

In addition to the above message types, there are also text messages, picture messages, voice messages, video messages, graphic card messages, etc. You can view the development document and configure the message body to be sent.

If you follow this app on WeChat, you can receive notifications on WeChat. If you have not followed, you can only receive notifications on WeChat.

Author

Author:TANKING
Web:www.likeyuns.com
WeChat:sansure2016


TANKING
4.8k 声望516 粉丝

热爱分享,热爱创作,热爱研究。