How to detect page redirection using PHP?

I am trying to determine if an image exists on a remote server. However, I have tried several methods and cannot make them work.

Now I'm trying to use this:

if (! CheckImageExists ("http://img2.netcarshow.com/ABT-Audi_R8_2008_1024x768_wallpaper_01.jpg")) {
    print_r ("DOES NOT EXIST");
} else {
    print_r ("DOES EXIST");
};

function CheckImageExists ($ imgUrl) {
    if (fopen ($ imgUrl, "r")) {
        return true;
    } else {
        return false;
    };
};

But it returns “true” whether the image really exists or not (the above image should, but change it to gibberish, and it will still return “true”). I feel this could be because if the URL does not exist, it will redirect to the website’s homepage. But I do not know how to detect this.

Thanks for any help!

+3
source share
4 answers

Use cURL .

After retrieving the resource, you can get an error code that calls curl_errno () .

+2
source

, HTML- $imgUrl, "404 " - .

, , .

0

This should do the trick (using image size):

if (!CheckImageExists("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png")) {
    echo 'DOES NOT EXIST';
} else {
    echo 'DOES EXIST';
};

function CheckImageExists($imgUrl) {
    if (@GetImageSize($imgUrl)) {
        return true;
    } else {
        return false;
    };
};
0
source

Got a job with the Seb method. I just used YQL to check the actual content of the page and determine if this is an error or not.

0
source

Source: https://habr.com/ru/post/1746426/


All Articles