WeChat enterprise account sends monitoring message [php], php

Source: Internet
Author: User

Enterprise ID sends monitoring message [php], php

The company has the function of abnormal monitoring and sending messages to the enterprise public account. Probably as follows:

1. scheduled task detection exception message 2. Saving the message to the redis queue 3. Scheduled processing of the queue exception message 4. sending it to the department group corresponding to the enterprise ID

Here, let's take a look at the sending process. The rest will not be discussed.

Simply put, two steps are required:

1. Obtain AccessToken2 and send the message to the corresponding Project Department group.

1. Obtain the AccessToken

The AccessToken is the globally unique ticket of the enterprise account. When calling an interface, you must carry the AccessToken.
The AccessToken must be replaced by the CorpID and Secret. Different Secret return different AccessToken. Under normal circumstances, the AccessToken is valid for 7200 seconds, and the same result is returned again during the validity period. Access_token retains at least 512 bytes of storage space.

Request description

Https Request Method: GEThttps: // qyapi.weixin.qq.com/cgi-bin/gettoken? Corpid = id & Cortana cret = secrect

Corpid enterprise Id
Credential key of the Cortana cret Management Group

Send messages

Enterprises can send messages to members.

Request description

Https Request Method: POSThttps: // qyapi.weixin.qq.com/cgi-bin/message/send? Access_token = ACCESS_TOKEN
Message applications support text, images, voice, video, files, text, and other message types. Homepage applications only support the text message type and contain up to 20 characters.

Parameters

  Required Description
Touser No List of Member IDS (message recipients. multiple receivers are separated by '|'. A maximum of 1000 recipients are supported ). Special case: If it is specified as @ all, it will be sent to all Members who follow the enterprise application
Toparty No Department ID list. multiple receivers are separated by '|'. A maximum of 100 recipients are supported. This parameter is ignored when touser is @ all.
Totag No Tag ID list. multiple receivers are separated by '|'. A maximum of 100 tags are supported. This parameter is ignored when touser is @ all.
Msgtype Yes Message type, which is fixed at this time as text (supports message-type applications and homepage applications)
Agentid Yes The id of the enterprise application, an integer. You can view it on the application settings page.
Content Yes Message content, up to 2048 bytes. Note: Text Messages pushed by homepage applications can only display up to 20 characters at the end (including Chinese and English)
Safe No Indicates whether the message is confidential, 0 indicates no, and 1 indicates yes. The default value is 0.

In fact, the process is quite simple. The simple code is as follows:

<? Php/*** public Account Information Processing */class WeixinMessage {// corpid public $ corpid = 'xxxxxx'; // sercret public $ corpsecret = 'xxxxx '; // send message api public $ weixinSendApi = 'https: // qyapi.weixin.qq.com/cgi-bin/message/send? Access_token = ';/*** request Api, get AccessToken */public function getAccessToken () {error_reporting (E_ALL); // temporary storage is not secure $ filePath = ROOT. 'cache/weix1_ken.txt '; $ tokenInfo = array (); if (is_file ($ filePath) {$ tokenInfo = json_decode (file_get_contents ($ filePath), TRUE);} if (! Isset ($ tokenInfo ['Access _ token']) | time ()> $ tokenInfo ['expires _ in']) {// update access_token $ getAccessTokenApi = "https://qyapi.weixin.qq.com/cgi-bin/gettoken? Corpid = {$ this-> corpid} & Cortana cret = {$ this-> Cortana cret} "; $ jsonString = $ this-> curlGet ($ getAccessTokenApi ); $ jsonInfo = json_decode ($ jsonString, true); if (isset ($ jsonInfo ['Access _ token']) {$ jsonInfo ['expires _ in'] = time () + 7100; file_put_contents ($ filePath, json_encode ($ jsonInfo) ;}$ tokenInfo = $ jsonInfo;} if (isset ($ tokenInfo ['Access _ token']) & $ tokenInfo ['expires _ in']> time () {return $ tokenInf O ['Access _ token'];} else {return FALSE ;}} /*** sending information interface ** @ author wanghan * @ param $ content sends content * @ param $ users received by touser @ all multiple users | separated * @ param $ toparty multiple groups received @ all are separated by | * @ param $ totag tag group @ all multiple tags | separated by * @ param $ agentid Application id * @ param $ msgtype information type text = simple text */public function send ($ content, $ touser = '@ all', $ toparty = '', $ totag ='', $ agentid = 8, $ msgtype = 'text ') {$ api = $ this-> weixinSendApi. $ This-> getAccessToken (); $ postData = array ('touser' => $ touser, 'toparty '=> $ toparty, 'totag' => $ totag, 'msgtype' => $ msgtype, 'agentid' => $ agentid, 'text' => array ('content' => urlencode ($ content ))); $ postString = urldecode (json_encode ($ postData); $ ret = $ this-> curlPost ($ api, $ postString); $ retArr = json_decode ($ ret, TRUE ); if (isset ($ retArr ['errcode']) & $ retArr ['errcode'] = 0) {return t Rue;} else {return false ;}} /*** Curl Post Data ** @ param string $ api for receiving data from url * @ param string $ data submitted by vars * @ param int $ second requires that the program be within $ second seconds done, run the * @ return string or boolean command in the background after $ second seconds, and the other party returns the */function curlPost ($ url, $ vars, $ second = 30) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_TIMEOUT, $ second); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_URL, $ url ); curl _ Setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ vars); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); // curl_setopt ($ ch, CURLOPT_HTTPHEADER, array (// 'content-Type: application/json; charset = UTF-8 ', // 'content-Length :'. strlen ($ vars) //); $ data = curl_exec ($ ch); curl_close ($ ch); if ($ data) return $ data; return false ;} /*** submit data in CURL get mode * submit api data in curl get mode * @ param s Tring $ url api address * @ param int $ second timeout time, measured in seconds * @ param string $ log_path: log storage path. If not, logs are not saved, the storage path must have read and write permissions * @ return true or false */function curlGet ($ url, $ second = 30, $ log_path = '', $ host = '', $ port = '') {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_TIMEOUT, $ second); curl_setopt ($ ch, CURLOPT_URL, $ url ); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CUR LOPT_SSL_VERIFYPEER, FALSE); if (! Empty ($ host) {curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ host);} if (! Empty ($ port) {curl_setopt ($ ch, CURLOPT_PORT, $ port);} $ data = curl_exec ($ ch); $ return_ch = curl_errno ($ ch ); curl_close ($ ch); if ($ return_ch! = 0) {if (! Empty ($ log_path) file_put_contents ($ log_path, curl_error ($ ch ). "\ n \ r", FILE_APPEND); return false;} else {return $ data ;}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.