You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

3 years ago
  1. <?php namespace WpsMcloud\Support;
  2. use Guzzle\Http\Client;
  3. use Guzzle\Http\Exception\ClientErrorResponseException;
  4. class Http
  5. {
  6. private static Client $guzzle;
  7. private static function getGuzzle(): Client
  8. {
  9. if (!isset(static::$guzzle)) {
  10. static::$guzzle = new Client();
  11. }
  12. return static::$guzzle;
  13. }
  14. public static function fileExists(string $url, ?\Closure $successCallback = null, ?\Closure $errorCallback = null): bool
  15. {
  16. try {
  17. $res = self::getGuzzle()->createRequest('HEAD', $url, null, null, [
  18. 'allow_redirects' => true,
  19. ])->send();
  20. if ($res->getStatusCode() == 200) {
  21. if ($successCallback !== null) {
  22. $successCallback();
  23. }
  24. return true;
  25. }
  26. } catch (ClientErrorResponseException $e) {
  27. if ($errorCallback !== null) {
  28. throw $e;
  29. }
  30. }
  31. if ($errorCallback !== null) {
  32. $errorCallback();
  33. }
  34. return false;
  35. }
  36. }