Alex
Alex

Reputation: 9265

curl to get headers

I want to check if a page gives a 200 header using curl.

I am using the following script:

public static function isUrlExist($url)
{
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_NOBODY, TRUE);
    curl_exec($curl);
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ($code == 200) {
        $status = true;
    } else {
        $status = false;
    }
    curl_close($curl);
    return $status;
}

The strange thing is it evaluates to true if I query a website such as vimeo: https://vimeo.com/api/oembed.json?url=https://vimeo.com/11896354

But websites such as Facebook or Google return false.

Am I missing something?

Upvotes: 0

Views: 82

Answers (1)

deadbeef
deadbeef

Reputation: 1301

My best guess is that facebook/google is redirecting you, causing a 3xx redirect status code. Try adding curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); before curl_exec() to make curl follow redirects.

Upvotes: 3

Related Questions