开发者

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 ind开发者_开发百科icator)

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


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.


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


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜