External URL image, database match, storage
Hello, I have a products table with an id that's matching a google storage id with some pictures. I am trying to create a script to download all the pictures to local storage so the page will load faster images.
But I am heading a problem with some id and pictures that doesn't match. I want them to be skipped but I am not able to skip them and the error will come up and stop the download.
I am getting XML response like this:
<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Details>
No such object
</Details>
</Error>
Inside the project, pop out:
file_get_contents(601028b15431515fd0e23833.png): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
But I am not able to skip this url response. What I use is simply:
$img = "external url";
$contents = file_get_contents($img);
$name = substr($img, strrpos($img, '/') + 1);
Storage::put($name, $contents);
What is the best method or way to deal with this problem? What I intend is just to match id from database, download all the files and use the storage for the pictures instead of external url.
Please or to participate in this conversation.