Solved

Check if a remote www-page is online...

Posted on 2006-11-19
3
200 Views
Last Modified: 2007-01-05
Hi,

I have a PHP-page that fetches information from another site, BUT the problem is that the site sometimes is not available or extremely slow. Hence, I would like to check if the site is online, prior to fetching the information from the site. How do I do that in the best way?

I have tried to use both
fsockopen("www.example.com",80);
and
file_get_contents()

Is there someway to set a timeout-limit?

Thanks in advance,
Jan
0
Comment
Question by:madwax
3 Comments
 
LVL 13

Accepted Solution

by:
rhickmott earned 300 total points
ID: 17976721
You shoul be able to do this in the fsock declaration.

fsockopen ( string target [, int port [, int &errno [, string &errstr [, float timeout]]]] )

Basically declare

fsockopen("www.example.com",80,$errno, $errstr,5)

will time out after 5 seconds and you can use the $errno and $errstr values to ask why. You can then do

if (!fsockopen("www.example.com",80,$errno, $errstr,5)) {
      echo("Site is dead");
} else {
     echo("Alive");
}

If you want to set a limit on how long your script spends reading data then you can use

http://us2.php.net/manual/en/function.stream-set-timeout.php

This function shoulw allow you to set a timelimit on streams
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now