The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Time Limit Or Alternative File Opening

I need a way to fopen a file from a remote server and read the data from the file into the script. The thing is, I want the script to either be able to return a custom error and continue loading if it can't open the file if the server is down. Currently, if I just fopen the file but the server is down, it takes forever to render the script or it doesn't finish rendering at all. I need a way to fix this. Is there any method?

I've read what you post... /you were faster than me LOL /
but I'm not sure about that script. It's using fopen, and i think it will normally try to open a remote file (url)...
but...
if the server isn't avaiable then it will hang until the connection times out. (is it in the php.ini?? as default I'm not sure :P)

but if you use sockets /fsockopen()/, then you'll be able to set custom timeouts in milliseconds /socket_set_timeout()/ for each funcion calls. Then the script can run much more faster, because you don't need to wait for the default timeout period.

I've read what you post... /you were faster than me LOL /
but I'm not sure about that script. It's using fopen, and i think it will normally try to open a remote file (url)...
but...
if the server isn't avaiable then it will hang until the connection times out. (is it in the php.ini?? as default I'm not sure :P)

Yeah, I haven't tested it myself (just a copy and paste), (thanx!)

Originally Posted by rae

but if you use sockets /fsockopen()/, then you'll be able to set custom timeouts in milliseconds /socket_set_timeout()/ for each funcion calls. Then the script can run much more faster, because you don't need to wait for the default timeout period.

That is difficult to solve if you don't know the status of the remote server. One solution might be to have a cronjob that creates a local copy of the file and your production script reads the local copy. Have the cronjob read the file as often as you need new data (ex. every 5 minutes). If you have multiple servers then you might assign the task to your least busy server and set it up so the file can be read by all your local computers. Hope the helps.

That is difficult to solve if you don't know the status of the remote server.

The CURL snippet above tests both the status of the server and the status of the requested file/page (200 OK status). Same can be achieved with fsockopen but it is not as fast. Every time I use CURL, I am impressed by its features.

Originally Posted by PHPCamp.com

One solution might be to have a cronjob that creates a local copy of the file and your production script reads the local copy. Have the cronjob read the file as often as you need new data (ex. every 5 minutes).

That's indeed the most practical solution but some application need to access data in real time. Some other scripts for example might test the dead links on a page. For that, fopen() can do the job but without timeout this may cause your application to freeze if the server is slow or fail to send the 200 OK status in a reasonable time. Further more, I believe that set_time_limit(), that changes the max_execution_time value, will not help as open streams are not accounted for in the execution time.

In my quest for a solution I found that functions like stream_set_timeout() are working fine with sockets but not for streams opened by fopen() or file_get_contents().

As of PHP 4.3, this function can (potentially) work on any kind of stream. In PHP 4.3, socket based streams are still the only kind supported in the PHP core, although streams from other extensions may support this function.http://www.php.net/manual/en/functio...et-timeout.php