downoad image using php and GuzzleHttp

In php we can use Guzzle library to download images.

Here I am using books.toscrape.com website as an example.

First, we have to install Guzzle library.

composer require guzzlehttp/guzzle:^7.0

and then include “autoload.php” file.

include 'vendor/autoload.php';

we need the “src” of the images we want to scrape.

use inspect element to find the address.

then instantiate a new Client class like so:


use GuzzleHttp\Client;

$client = new Client();

i saved the image address in $src and destination in $filepath.

Cient class of guzzle has a get method which we use it like so to download the image:

$src = 'https://books.toscrape.com/media/cache/3e/ef/3eef99c9d9adef34639f510662022830.jpg';

$file_path = 'thumbnail.jpg';

$response = $client->get($src, ['sink' => $file_path]);

the first parameter is the image address and the second one is an array with a key named “sink” and value that should be the destination path.

now, if we run this code we will have the image downloaded in our project’s root.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *