Crash893
Crash893

Reputation: 11712

Test if webpage is responding via php

I'm trying to build a basic "status" page using php that will tell my users if various services (webpages we use) are at least serving up pages (which isn't 100% guarantee of working but its pretty good indicator)

what i would like to do is something like

www.domainname.com/mediawiki/index.php and make sure that returns the page or not

I'm pretty new to php so im not even sure what function im looking for.

Thanks

Upvotes: 1

Views: 3037

Answers (4)

Jason
Jason

Reputation: 15365

Another option would be to see of the socket is responding. (I can't remember where I got this from originally but it lets me know if port 80 is responding). You could always direct this to a different port.

function server($addr){
    if(strstr($addr,'/')){$addr = substr($addr, 0, strpos($addr, '/'));}
    return $addr;
};    

$link    = 'secure.sdinsite.net:';
$s_link  = str_replace('::', ':', $link);
$address = explode (':',"$s_link");
$churl   = @fsockopen(server($addrress[0]), 80, $errno, $errstr, 20);

if (!$churl) {
    $status = 'dead';
} else {
    $status = 'live';
};

echo $status;

Upvotes: 0

webbiedave
webbiedave

Reputation: 48887

Check out file_get_contents

It will return the web page source as a string. This way you could even search the string for a specific value, if so desired, for finer results. This can be very useful in case content is still returned but is some sort of error message.

$somePage = file_get_contents('http://www.domainname.com/mediawiki/index.php');
// $somePage now contains the HTML source or false if failed

Ensure allow_url_fopen = On in your php.ini

If you need to check the response headers, you can use $http_response_header

Upvotes: 0

Cybrix
Cybrix

Reputation: 3318

Try this:

<?php
    $_URL = "http://www.domainname.com/mediawiki/index.php";

    if (! @file_get_contents($_URL))
    {
        echo "Service not responding.";
    }
?>

Note that your php.ini must activate allow_url_fopen

Good luck

Upvotes: 0

Eli
Eli

Reputation: 5630

There are ways to use built-in PHP functions to do this (e.g. file_get_contents), but they aren't very good. I suggest you take a look at the excellent cURL library. This might point you in the right direction: Header only retrieval in php via curl

Since you just want to see if a page is "up" you don't need to request the whole page, you can just use a HEAD request to get the headers for the page.

Upvotes: 5

Related Questions