1

currently i am using following function

function urlExist($url)
{
                $handle   = curl_init($url);
                if (false === $handle)
                {
                        return false;
                }
                curl_setopt($handle, CURLOPT_HEADER, false);
                curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
                curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox
                curl_setopt($handle, CURLOPT_NOBODY, true);
                curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
                $connectable = curl_exec($handle);
                ##print $connectable;
                curl_close($handle);
                return $connectable;
}

It works fine for simple url but does not work for url that redirects to another domain

2 Answers 2

6

You need to setFOLLOWLOCATION:

curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);

However, there is no point in issuing a GET request here. A simple HEAD is lighter since only headers are transferred. To do that, set NOBODY to true:

curl_setopt($handle, CURLOPT_NOBODY, true);
Sign up to request clarification or add additional context in comments.

Comments

0

I'm using the same function but i'm not meeting problem with domain trieuvieclam.com redirect to new domain. I'm using CHROME browser

function url_exists($url) {
    $handle   = curl_init($url);
    if (false === $handle)
    {
            return false;
    }

    curl_setopt($handle, CURLOPT_HEADER, false);
    curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
    curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox
    curl_setopt($handle, CURLOPT_NOBODY, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 3);
    $connectable = curl_exec($handle);
    ##print $connectable;
    curl_close($handle);
    if($connectable){
        return true;
    }
    return false;
}

I have tried change this line :curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true); to false, but the result is still the same (existing the domain).

if (false === $handle)
        {
                return false;
        }

This condition never meet for any string, even not an url, maybe it only match when curl is not supported in the server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.