What is the best way to use Guzzle to check for a remote file?

I would like to use Guzzle to check for the presence of a remote file.

This is an example of how I am checking now:

/** * @return boolean */ function exists() { // By default get_headers uses a GET request to fetch the headers. // Send a HEAD request instead stream_context_set_default( array( 'http' => array( 'method' => 'HEAD' ) ) ); // Get the file headers $file_headers = @get_headers($this->file); // Check file headers for 404 if($file_headers[0] == 'HTTP/1.1 404 Not Found') return false; // File not available. return true; // File is available! } 

However, since I already use Guzzle elsewhere, I think I can make it more beautiful and more readable.

Am I right in thinking about this? How can i do this?

+5
source share
1 answer

I managed to find part of the answer in the docs. Guzzle - request methods

In combination with gist , which has a similar function that checks the status of 404.

 /** * @return boolean */ function exists() { $client = new GuzzleHttp\Client; try { $client->head($this->file); return true; } catch (GuzzleHttp\Exception\ClientException $e) { return false; } } 
+8
source

Source: https://habr.com/ru/post/1205471/


All Articles