Mesaber
Mesaber

Reputation: 57

How do I ping a webpage in php to make sure it's alive?

I have a list of urls for some webpages and I want to make sure that this url ( webpage ) exists and not deleted or it doesn't exist. I want to do it in PHP.

How do I ping a webpage to make sure it's alive?

Upvotes: 2

Views: 504

Answers (3)

alex
alex

Reputation: 490263

$urls = array(...);

foreach($urls as $url) {
    $headers = get_headers($url);

    if ( ! $headers OR strpos($headers[0], '200 OK') === FALSE) {
       // Site is down.
    } 
}

Alternatively you could use ping.

$response = shell_exec('ping ' . escapeshellarg($url));

// Parse $response.

Update

You mention you want this to be scheduled. Have a look into cron jobs.

Upvotes: 3

tofutim
tofutim

Reputation: 23374

See http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786 for a thorough implementation.

Upvotes: 0

Brian Webster
Brian Webster

Reputation: 30855

Create a PHP script that fires off an HTTP Request to each URL that you want to be kept-alive.

PHP HTTP Request

I suggest setting up a Task in your operating system that accesses this script every 15 minutes in order to keep these applications alive. Here's some info on running PHP from the command line in Windows.

Upvotes: 1

Related Questions