LoginSignup
3
3

More than 5 years have passed since last update.

check for broken link

Last updated at Posted at 2016-05-20

You can check for broken link using this function:

function check_url($url) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    $headers = curl_getinfo($ch);
    curl_close($ch);

    return $headers['http_code'];
}

You need to have CURL installed for this to work. Now you can check for broken links using:

$check_url_status = check_url($url);
if ($check_url_status == '200')
   echo "Link Works";
else
   echo "Broken Link";
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3