I am trying to download images from a server, but sometimes the images are not found (the server returns a "404") so I want to download a placeholder image or just skip this image URL. What I have now is:
foreach($images as $image){
$handle = curl_init($image);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
$image = "https://placehold.it/1200x800";
}
curl_close($handle);
$http_client = new Client(array(
'base_uri' => '',
'verify' => false,
));
try {
$res = $http_client->get($image);
$name = substr($image, strrpos($image, '/') + 1);
Storage::put($vendor_code."/".$name, $res->getBody());
} catch (Exception $ex) {
\Log::error($ex);
}
}
But even so, I still get an exception that the image is not found
Any help, please?
Thanks