• Reading time ~ 1 min
  • 26.06.2023

Marcel Pociot shared a tip on using the Laravel HTTP client method sink() to write a response to a file:

I just learned about a neat little undocumented @laravelphp HTTP client method.

The "sink" method allows you to pass a filename or resource. The response to the request will then be written to this file.

This makes it super easy to download remote files with additional headers pic.twitter.com/ZHHURltcfA

— Marcel Pociot 🧪 (@marcelpociot) June 20, 2023

The sink request option is also available in the Guzzle HTTP client library, formerly the save_to request option:

$client->request('GET', '/stream/20', ['sink' => '/path/to/file']);

// PHP stream
$resource = \GuzzleHttp\Psr7\Utils::tryFopen('/path/to/file', 'w');
$stream = \GuzzleHttp\Psr7\Utils::streamFor($resource);
$client->request('GET', '/stream/20', ['sink' => $stream]);

If you'd like to learn more about all the excellent features Laravel's HTTP request class provides, check out the HTTP Requests documentation. The PendingRequest source is another excellent resource to see how Laravel's HTTP client works under the hood.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297