|
14 | 14 | use craft\helpers\UrlHelper;
|
15 | 15 | use GuzzleHttp\Client;
|
16 | 16 | use GuzzleHttp\RequestOptions;
|
| 17 | +use Psr\Http\Message\ResponseInterface; |
17 | 18 | use Throwable;
|
18 | 19 | use yii\caching\ChainedDependency;
|
19 | 20 | use yii\caching\FileDependency;
|
@@ -78,45 +79,54 @@ public static function fetch(string $pathOrUrl, callable $callback = null, strin
|
78 | 79 | self::CACHE_KEY . $cacheKeySuffix . $pathOrUrl,
|
79 | 80 | function () use ($pathOrUrl, $callback) {
|
80 | 81 | $contents = null;
|
81 |
| - $result = null; |
82 | 82 | if (UrlHelper::isAbsoluteUrl($pathOrUrl)) {
|
83 |
| - // See if we can connect to the server |
84 |
| - $clientOptions = [ |
85 |
| - RequestOptions::HTTP_ERRORS => false, |
86 |
| - RequestOptions::CONNECT_TIMEOUT => 3, |
87 |
| - RequestOptions::VERIFY => false, |
88 |
| - RequestOptions::TIMEOUT => 5, |
89 |
| - ]; |
90 |
| - $client = new Client($clientOptions); |
91 |
| - try { |
92 |
| - $response = $client->request('GET', $pathOrUrl, [ |
93 |
| - RequestOptions::HEADERS => [ |
94 |
| - 'Accept' => '*/*', |
95 |
| - ], |
96 |
| - ]); |
97 |
| - if ($response->getStatusCode() === 200) { |
98 |
| - $contents = $response->getBody()->getContents(); |
99 |
| - } |
100 |
| - } catch (Throwable $e) { |
101 |
| - Craft::error($e->getMessage(), __METHOD__); |
| 83 | + $response = self::fetchResponse($pathOrUrl); |
| 84 | + if ($response && $response->getStatusCode() === 200) { |
| 85 | + $contents = $response->getBody()->getContents(); |
102 | 86 | }
|
103 | 87 | } else {
|
104 | 88 | $contents = @file_get_contents($pathOrUrl);
|
105 | 89 | }
|
106 |
| - if ($contents) { |
107 |
| - $result = $contents; |
108 |
| - if ($callback) { |
109 |
| - $result = $callback($result); |
110 |
| - } |
| 90 | + if ($contents && $callback) { |
| 91 | + $contents = $callback($contents); |
111 | 92 | }
|
112 | 93 |
|
113 |
| - return $result; |
| 94 | + return $contents; |
114 | 95 | },
|
115 | 96 | $cacheDuration,
|
116 | 97 | $dependency
|
117 | 98 | );
|
118 | 99 | }
|
119 | 100 |
|
| 101 | + /** |
| 102 | + * Return a Guzzle ResponseInterface for the passed in $url |
| 103 | + * |
| 104 | + * @param string $url |
| 105 | + * @return ResponseInterface|null |
| 106 | + */ |
| 107 | + public static function fetchResponse(string $url): ?ResponseInterface |
| 108 | + { |
| 109 | + $response = null; |
| 110 | + $clientOptions = [ |
| 111 | + RequestOptions::HTTP_ERRORS => false, |
| 112 | + RequestOptions::CONNECT_TIMEOUT => 3, |
| 113 | + RequestOptions::VERIFY => false, |
| 114 | + RequestOptions::TIMEOUT => 5, |
| 115 | + ]; |
| 116 | + $client = new Client($clientOptions); |
| 117 | + try { |
| 118 | + $response = $client->request('GET', $url, [ |
| 119 | + RequestOptions::HEADERS => [ |
| 120 | + 'Accept' => '*/*', |
| 121 | + ], |
| 122 | + ]); |
| 123 | + } catch (Throwable $e) { |
| 124 | + Craft::error($e->getMessage(), __METHOD__); |
| 125 | + } |
| 126 | + |
| 127 | + return $response; |
| 128 | + } |
| 129 | + |
120 | 130 | /**
|
121 | 131 | * Combine a path with a URL to create a URL
|
122 | 132 | *
|
|
0 commit comments