ServerChan.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Components;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Psr7;
  5. use GuzzleHttp\Exception\RequestException;
  6. use Log;
  7. class ServerChan
  8. {
  9. protected static $systemConfig;
  10. function __construct()
  11. {
  12. self::$systemConfig = Helpers::systemConfig();
  13. }
  14. /**
  15. * 推送消息
  16. *
  17. * @param string $title 消息标题
  18. * @param string $content 消息内容
  19. *
  20. * @return mixed
  21. * @throws \GuzzleHttp\Exception\GuzzleException
  22. */
  23. public function send($title, $content)
  24. {
  25. $client = new Client();
  26. try {
  27. $response = $client->request('GET', 'https://sc.ftqq.com/' . self::$systemConfig['server_chan_key'] . '.send', [
  28. 'query' => [
  29. 'text' => $title,
  30. 'desp' => $content
  31. ]
  32. ]);
  33. $result = json_decode($response->getBody());
  34. if (!$result->errno) {
  35. Helpers::addEmailLog(1, '[ServerChan]' . $title, $content);
  36. } else {
  37. Helpers::addEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
  38. }
  39. } catch (RequestException $e) {
  40. Log::error(Psr7\str($e->getRequest()));
  41. if ($e->hasResponse()) {
  42. Log::error(Psr7\str($e->getResponse()));
  43. }
  44. }
  45. }
  46. }