Response.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @package Grav\Common\HTTP
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\HTTP;
  9. use Exception;
  10. use Grav\Common\Utils;
  11. use Grav\Common\Grav;
  12. use Symfony\Component\HttpClient\CurlHttpClient;
  13. use Symfony\Component\HttpClient\Exception\TransportException;
  14. use Symfony\Component\HttpClient\HttpClient;
  15. use Symfony\Component\HttpClient\HttpOptions;
  16. use Symfony\Component\HttpClient\NativeHttpClient;
  17. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  18. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  19. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  20. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  21. use Symfony\Contracts\HttpClient\HttpClientInterface;
  22. use Symfony\Contracts\HttpClient\ResponseInterface;
  23. use function call_user_func;
  24. use function defined;
  25. /**
  26. * Class Response
  27. * @package Grav\Common\GPM
  28. */
  29. class Response
  30. {
  31. /**
  32. * Backwards compatible helper method
  33. *
  34. * @param string $uri
  35. * @param array $overrides
  36. * @param callable|null $callback
  37. * @return string
  38. * @throws TransportExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|TransportExceptionInterface|ClientExceptionInterface
  39. */
  40. public static function get(string $uri = '', array $overrides = [], callable $callback = null): string
  41. {
  42. $response = static::request('GET', $uri, $overrides, $callback);
  43. return $response->getContent();
  44. }
  45. /**
  46. * Makes a request to the URL by using the preferred method
  47. *
  48. * @param string $method method to call such as GET, PUT, etc
  49. * @param string $uri URL to call
  50. * @param array $overrides An array of parameters for both `curl` and `fopen`
  51. * @param callable|null $callback Either a function or callback in array notation
  52. * @return ResponseInterface
  53. * @throws TransportExceptionInterface
  54. */
  55. public static function request(string $method, string $uri, array $overrides = [], callable $callback = null): ResponseInterface
  56. {
  57. if (empty($method)) {
  58. throw new TransportException('missing method (GET, PUT, etc.)');
  59. }
  60. if (empty($uri)) {
  61. throw new TransportException('missing URI');
  62. }
  63. // check if this function is available, if so use it to stop any timeouts
  64. try {
  65. if (Utils::functionExists('set_time_limit')) {
  66. @set_time_limit(0);
  67. }
  68. } catch (Exception $e) {}
  69. $client = Client::getClient($overrides, 6, $callback);
  70. return $client->request($method, $uri);
  71. }
  72. /**
  73. * Is this a remote file or not
  74. *
  75. * @param string $file
  76. * @return bool
  77. */
  78. public static function isRemote($file): bool
  79. {
  80. return (bool) filter_var($file, FILTER_VALIDATE_URL);
  81. }
  82. }