connect(REDIS_HOST, REDIS_PORT); } return $GLOBALS['REDIS_INSTANCE']; } function send_to_wecom($text, $wecom_cid, $wecom_secret, $wecom_aid, $wecom_touid = '@all') { $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) { $access_token = $info['access_token']; } } if ($access_token) { $url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.urlencode($access_token); $data = new \stdClass(); $data->touser = $wecom_touid; $data->agentid = $wecom_aid; $data->msgtype = "text"; $data->text = ["content"=> $text]; $data->duplicate_check_interval = 600; $data_json = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); if ($response !== false && REDIS_ON) { redis()->set(REDIS_KEY, $access_token, ['nx', 'ex'=>REDIS_EXPIRED]); } return $response; } return false; }