新增带access token缓存的实现。
This commit is contained in:
parent
fbe08a0d97
commit
692faa93e9
|
@ -0,0 +1,2 @@
|
||||||
|
demo.php
|
||||||
|
show.php
|
|
@ -60,6 +60,8 @@ PS:如果出现`接口请求正常,企业微信接受消息正常,个人
|
||||||
|
|
||||||
#### 第五步,通过以下函数发送消息:
|
#### 第五步,通过以下函数发送消息:
|
||||||
|
|
||||||
|
PS:为使用方便,以下函数没有对 `access_token` 进行缓存。对于个人低频调用已经够用。带缓存的实现可查看 `index.php` 中的示例代码(依赖Redis实现)。
|
||||||
|
|
||||||
PHP版:
|
PHP版:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
|
45
index.php
45
index.php
|
@ -1,11 +1,22 @@
|
||||||
<?php
|
<?php
|
||||||
|
// config
|
||||||
|
// ======================================
|
||||||
define('SENDKEY', 'set_a_sendkey');
|
define('SENDKEY', 'set_a_sendkey');
|
||||||
define('WECOM_CID', '企业微信公司ID');
|
define('WECOM_CID', '企业微信公司ID');
|
||||||
define('WECOM_SECRET', '企业微信应用Secret');
|
define('WECOM_SECRET', '企业微信应用Secret');
|
||||||
define('WECOM_AID', '企业微信应用ID');
|
define('WECOM_AID', '企业微信应用ID');
|
||||||
define('WECOM_TOUID', '@all');
|
define('WECOM_TOUID', '@all');
|
||||||
|
|
||||||
|
// 以下配置需要有 redis 服务和 phpredis 扩展
|
||||||
|
define('REDIS_ON', false);
|
||||||
|
define('REDIS_HOST', '127.0.0.1');
|
||||||
|
define('REDIS_PORT', '6379');
|
||||||
|
define('REDIS_EXPIRED', '7000');
|
||||||
|
define('REDIS_KEY', 'wecom_access_token');
|
||||||
|
|
||||||
|
// code
|
||||||
|
// ======================================
|
||||||
|
|
||||||
if (strlen(@$_REQUEST['sendkey']) < 1
|
if (strlen(@$_REQUEST['sendkey']) < 1
|
||||||
|| strlen(@$_REQUEST['text']) < 1 || @$_REQUEST['sendkey'] != SENDKEY
|
|| strlen(@$_REQUEST['text']) < 1 || @$_REQUEST['sendkey'] != SENDKEY
|
||||||
) {
|
) {
|
||||||
|
@ -16,12 +27,33 @@ header("Content-Type: application/json; charset=UTF-8");
|
||||||
echo send_to_wecom(@$_REQUEST['text'], WECOM_CID, WECOM_SECRET, WECOM_AID, WECOM_TOUID);
|
echo send_to_wecom(@$_REQUEST['text'], WECOM_CID, WECOM_SECRET, WECOM_AID, WECOM_TOUID);
|
||||||
|
|
||||||
|
|
||||||
|
function redis()
|
||||||
|
{
|
||||||
|
if (!isset($GLOBALS['REDIS_INSTANCE']) || !$GLOBALS['REDIS_INSTANCE']) {
|
||||||
|
$GLOBALS['REDIS_INSTANCE'] = new Redis();
|
||||||
|
$GLOBALS['REDIS_INSTANCE']->connect(REDIS_HOST, REDIS_PORT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $GLOBALS['REDIS_INSTANCE'];
|
||||||
|
}
|
||||||
|
|
||||||
function send_to_wecom($text, $wecom_cid, $wecom_secret, $wecom_aid, $wecom_touid = '@all')
|
function send_to_wecom($text, $wecom_cid, $wecom_secret, $wecom_aid, $wecom_touid = '@all')
|
||||||
{
|
{
|
||||||
$info = @json_decode(file_get_contents("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".urlencode($wecom_cid)."&corpsecret=".urlencode($wecom_secret)), true);
|
$access_token = false;
|
||||||
|
// 如果启用redis作为缓存
|
||||||
|
if (REDIS_ON) {
|
||||||
|
$access_token = redis()->get(REDIS_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$access_token) {
|
||||||
|
$info = @json_decode(file_get_contents("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".urlencode($wecom_cid)."&corpsecret=".urlencode($wecom_secret)), true);
|
||||||
|
|
||||||
if ($info && isset($info['access_token']) && strlen($info['access_token']) > 0) {
|
if ($info && isset($info['access_token']) && strlen($info['access_token']) > 0) {
|
||||||
$access_token = $info['access_token'];
|
$access_token = $info['access_token'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($access_token) {
|
||||||
$url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.urlencode($access_token);
|
$url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.urlencode($access_token);
|
||||||
$data = new \stdClass();
|
$data = new \stdClass();
|
||||||
$data->touser = $wecom_touid;
|
$data->touser = $wecom_touid;
|
||||||
|
@ -44,7 +76,12 @@ function send_to_wecom($text, $wecom_cid, $wecom_secret, $wecom_aid, $wecom_toui
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
|
if ($response !== false && REDIS_ON) {
|
||||||
|
redis()->set(REDIS_KEY, $access_token, REDIS_EXPIRED);
|
||||||
|
}
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue