0

Possible Duplicate:
How can one check to see if a remote file exists using PHP?

I want to programatically check if a website is live or not. I know i can do this by opening the url using "cURL" or "fopen" but it takes a lot of time because it needs to fetch the full page.

Furthermore, this method is not reliable because there can be other reasons like unsupported protocols to be able to open the website.

Is there any other way??

Community
  • 1
  • 1
Sourabh
  • 1,757
  • 6
  • 21
  • 43

4 Answers4

3

You could simply use HEAD request to get only the headers of the page and not the whole page. Still, the website will still generate the full page but at least you won't download everything.

To achieve this, you can use many methods, just check how to change the headers of the request and instead of doing a GET, you can do a HEAD.

Mathieu Dumoulin
  • 12,126
  • 7
  • 43
  • 71
  • but it will take the same amount of time as to open the website, is it more reliable then fopen or curl? – Sourabh Jul 17 '12 at 19:26
  • As long as you trigger the HTTP protocol it will do the whole page if the server/website is not built to return only the headers. What you want is some kind of watchdog but usually, people will implement a small webpage that only listens for a request and return something to state they are alive. – Mathieu Dumoulin Jul 17 '12 at 19:27
  • If you can't rely on such a mechanism and must get the homepage, then there is nothing you can do more, get the homepage, wait for the all the data to be processed, but using HEAD you can only transfer a portion of the response and not get everything. – Mathieu Dumoulin Jul 17 '12 at 19:28
1

fopen() and fread() do not read the entire webpage (not necessarily anyway). You can use that and read only a few bytes to determine the website exists (200 OK).

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

You could just send a header request and check the http response codes?

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
  • can you please elaborate your answer? – Sourabh Jul 17 '12 at 19:12
  • Im not really sure what else there is to elaborate on. It would be quite easy from my answer to go and do your follow up research using google. – Jon Taylor Jul 17 '12 at 19:12
  • 1
    @JonTaylor: What is a header request? How do you send one? How do you check the HTTP response codes? Just because it's obvious for you, doesn't mean it is for eeveryone else :) – Madara's Ghost Jul 17 '12 at 19:14
  • 1
    @Truth No I never said it was obvious, but if they don't do a bit of work themselves theres no point, Ive given them the right idea, they can more than easily search google to find out how to implement it. I'm not going to feed them all the answers, but I am more than happy (as you can tell from most of my answers on SO) to point people in the right direction. – Jon Taylor Jul 17 '12 at 19:16
  • If they are unwilling to type "php header request" and get this http://www.google.co.uk/webhp?sourceid=chrome-instant&ie=UTF-8#hl=en&safe=off&output=search&sclient=psy-ab&q=php%20header%20request&oq=&gs_l=&pbx=1&fp=2d4c3ea199c03db2&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&biw=1097&bih=550 back, then I don't see why I should spend my time typing the answers out for them. – Jon Taylor Jul 17 '12 at 19:20
  • @JonTaylor yeah u r right, spoon feeding is not what i wanted. – Sourabh Jul 17 '12 at 19:20
  • I am of course more than willing to help anyone who has a problem or a question that I feel I can help with, and I feel that I did point @Sourabh in the correct direction, the same direction that every other answer has pointed them in. – Jon Taylor Jul 17 '12 at 19:22
0
$file = 'http://www.test.com/idontexist.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}
Vinit
  • 1,815
  • 17
  • 38