0

I need to check the given short url is active or not. and used the following php function to get the url information, after get the expanded url.

get_headers($original_url);

And the result is not correct. I test this using the currently active url.

Any other way to find?

Thanks in advance.

Maheswari
  • 131
  • 1
  • 2
  • 8
  • 4
    Describe `active url` – Justinas Feb 11 '15 at 07:53
  • "Not correct" basically amounts to "it's not working", which is of little use to people when it comes to determining the cause of a problem. What is your input data, what results were you expecting, and what results did you actually get? – GordonM Feb 11 '15 at 07:57
  • Expanded url? active url? original url? Please can you explain this like you are explaining it to somebody who has never seen your system before or knows what it is supposed to do. Thanks in advance. – Antony D'Andrea Feb 11 '15 at 08:00

1 Answers1

0

To check if remote file exist or not use cUrl for that:

$ch = curl_init('http://example.com/index.php');

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.

if ($retcode == 200) {
    $isUrlOk = true;
} else {
    $isUrlOk = false;
}

curl_close($ch);
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • 2
    This isn't a bad solution but it could stand some improvement. The remote server can return plenty of non-error codes that aren't 200. For example redirects can result in a 3xx code. Also you could improve efficiency by making the request a HEAD request so it only fetches the headers and not the entire page. – GordonM Feb 11 '15 at 08:01