Last active
August 11, 2021 07:00
-
-
Save bootell/36cf1d61228bea9f1f28533f98e9ef0c to your computer and use it in GitHub Desktop.
企业微信推送消息
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Wework | |
{ | |
const CORPID = ''; | |
const CORPSECRET = ''; | |
const AGENTID = 1000002; | |
protected $_access_token; | |
/** | |
* Get access_token | |
* | |
* @link https://work.weixin.qq.com/api/doc/90001/90143/90605 | |
* @return mixed | |
* @throws Exception | |
*/ | |
public function getAccessToken() | |
{ | |
if (!$this->_access_token) { | |
$api = strtr('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET', [ | |
'ID' => static::CORPID, | |
'SECRET' => static::CORPSECRET, | |
]); | |
$response = file_get_contents($api); | |
$result = json_decode($response, true); | |
if (!isset($result['errcode']) || $result['errcode'] != 0) { | |
throw new Exception('get access_token failed: ' . $response); | |
} | |
$this->_access_token = $result['access_token']; | |
} | |
return $this->_access_token; | |
} | |
/** | |
* Get user_id by mobile number | |
* | |
* @link https://work.weixin.qq.com/api/doc/90001/90143/91693 | |
* @param $mobile | |
* @return mixed | |
* @throws Exception | |
*/ | |
public function getUserId($mobile) | |
{ | |
$api = strtr('https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=ACCESS_TOKEN', [ | |
'ACCESS_TOKEN' => $this->getAccessToken(), | |
]); | |
$ch = curl_init($api); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['mobile' => $mobile])); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$response = curl_exec($ch); | |
if (!$response) { | |
throw new Exception('Get UserId request failed: (' . curl_errno($ch) . ')' . curl_error($ch)); | |
} | |
curl_close($ch); | |
$result = json_decode($response, true); | |
if (!isset($result['errcode']) || $result['errcode'] != 0) { | |
throw new Exception('Get UserId failed: ' . $response); | |
} | |
return $result['userid']; | |
} | |
public function sendText($touser, $content) | |
{ | |
$data = [ | |
'touser' => $touser, | |
'msgtype' => 'text', | |
'agentid' => static::AGENTID, | |
'text' => [ | |
'content' => $content, | |
] | |
]; | |
$this->sendMessage($data); | |
} | |
public function sendTextCard($touser, $title, $description, $url = null) | |
{ | |
$data = [ | |
'touser' => $touser, | |
'msgtype' => 'textcard', | |
'agentid' => static::AGENTID, | |
'textcard' => [ | |
'title' => $title, | |
'description' => $description, | |
'url' => $url, | |
] | |
]; | |
$this->sendMessage($data); | |
} | |
protected function sendMessage($data) | |
{ | |
$api = strtr('https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN', [ | |
'ACCESS_TOKEN' => $this->getAccessToken(), | |
]); | |
$ch = curl_init($api); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$response = curl_exec($ch); | |
if (!$response) { | |
throw new Exception('Send message request failed: (' . curl_errno($ch) . ')' . curl_error($ch)); | |
} | |
curl_close($ch); | |
$result = json_decode($response, true); | |
if (!isset($result['errcode']) || $result['errcode'] != 0) { | |
throw new Exception('Send message failed: ' . $response); | |
} | |
} | |
} | |
$wework = new Wework(); | |
$wework->sendText('@all', '你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。'); | |
$wework->sendTextCard('@all', '领奖通知', "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment