I have a function which runs through an array of URL images, taken from my database, to see if they exist in order to upload images which do exist to another place. The application is in PHP using CakePhp 2.x and I'm using a function which I found on another Stackoverflow question.
The function which checks if the image exist is the next:
public function checkImageContentType($url) {
return (@fopen($url,"r")==true);
}
But I've also test the next code:
function checkRemoteFile($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
if($result !== FALSE)
{
return true;
}
else
{
return false;
}
}
Its worth mentioning the images are from a remote domain so I can not use file_exist function. The code in question which runs the for of the array of url images and calls the functions to check if the images exist is the next:
foreach($inventories as $inventory) {
if (!($this->checkImageContentType($inventory['Inventory']['imgsrc1']))) {
echo "<p>Pass: ".$inventory['Inventory']['sku']." <a href='". $inventory['Inventory']['imgsrc1'] ."'>IMG Here</a> </p>";
continue;
}
echo "<p>Product SKU:".$inventory['Inventory']['sku']."</p>";
....
}
If the image exists it keeps on with the function to upload, if not it continues on with the next image in array. The only problem is this code is not detecting all images for some reason. It passes on over some images which do exists and detects only a few images, this happens randomly I'm not sure why. Any ideas what I'm doing wrong or what I could try to debug this?
EDIT:
I think I found the reason, its making to many calls to the other server, so it is sending me a 403 error. Is there a way to overcome this? I'm making many calls to the other server I think that is why the other server is preventing me from making any more calls.
EDIT 2:
Just to give a little more context, the images URL where stored on Shopify Files and they have a hot link protection in order to prevent many calls from a single server. So you cant check out that many links even if the links are 404s. And there is a cooldown where Shopify wont allow you to check any more Shopify URLs, hence the 403 error. Or at least that is what I think is happening. All I had to do was keep the request to a minimum and well expand the times of my app checking for the images url from Shopify. Maybe its not the best solution, but for now it was what I found out.
If someone knows how to bypass or workaround the HotLink Protection I would like to hear the ideas :P