Can anyone tell me if its possible to validate a link with php? By validate, I mean check if the link is active and works not just the actual format of the link.
You need to do a HEAD request and check the response. 200 indicates the request succeeded. There are others that can be found here that you may want to treat as valid. (301 and 302 redirects spring to mind)
If you use cURL, you could use something like this
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); //Include the headers
curl_setopt($ch, CURLOPT_NOBODY, TRUE); //Make HEAD request
$response = curl_exec($ch);
if ( $response === false ){
//something went wrong, assume not valid
}
//list of status codes you want to treat as valid:
$validStatus = array(200, 301, 302, 303, 307);
if( !in_array(curl_getinfo($ch, CURLINFO_HTTP_CODE), $validStatus) ) {
//the HTTP code is not valid. The url is not valid
}
curl_close($ch);