From 35df80aa218a1331c7f1fa72aa9b65af91135926 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Fri, 10 Mar 2023 01:08:46 +0800 Subject: [PATCH] update api url --- app/Module/Extranet.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app/Module/Extranet.php b/app/Module/Extranet.php index 06c691271..de4958c4b 100644 --- a/app/Module/Extranet.php +++ b/app/Module/Extranet.php @@ -275,7 +275,7 @@ class Extranet case "soup": $text = "鸡汤分完了"; $data = self::curl('https://api.ayfre.com/jt/?type=bot', 5); - if ($data = trim($data)) { + if ($data) { $text = "心灵鸡汤:{$data}"; } break; @@ -285,22 +285,27 @@ class Extranet /** * @param $url - * @param int $cacheSecond + * @param int $cacheSecond 缓存时间(秒),如果结果为空则缓存有效30秒 * @param int $timeout * @return string */ private static function curl($url, int $cacheSecond = 0, int $timeout = 15): string { if ($cacheSecond > 0) { - $res = Cache::remember("curlCache::" . md5($url), Carbon::now()->addSeconds($cacheSecond), function () use ($timeout, $url) { - return Ihttp::ihttp_request($url, [], [], $timeout); + $key = "curlCache::" . md5($url); + $content = Cache::remember($key, Carbon::now()->addSeconds($cacheSecond), function () use ($cacheSecond, $key, $timeout, $url) { + $result = Ihttp::ihttp_request($url, [], [], $timeout); + $content = Base::isSuccess($result) ? trim($result['data']) : ''; + if (empty($content) && $cacheSecond > 30) { + Cache::put($key, "", Carbon::now()->addSeconds(30)); + } + return $content; }); } else { - $res = Ihttp::ihttp_request($url, [], [], $timeout); + $result = Ihttp::ihttp_request($url, [], [], $timeout); + $content = Base::isSuccess($result) ? trim($result['data']) : ''; } - if (Base::isSuccess($res)) { - return $res['data']; - } - return ''; + // + return $content; } }