ServerChan.php 931 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. /**
  10. * @param string $title 消息标题
  11. * @param string $content 消息内容
  12. * @param string $key ServerChan上申请的SCKEY
  13. * @return string
  14. */
  15. public function send($title, $content, $key)
  16. {
  17. $client = new Client();
  18. try {
  19. $response = $client->request('GET', 'https://sc.ftqq.com/' . $key . '.send', [
  20. 'query' => [
  21. 'text' => $title,
  22. 'desp' => $content
  23. ]
  24. ]);
  25. return json_decode($response->getBody());
  26. } catch (RequestException $e) {
  27. Log::error(Psr7\str($e->getRequest()));
  28. if ($e->hasResponse()) {
  29. Log::error(Psr7\str($e->getResponse()));
  30. }
  31. }
  32. }
  33. }