Brian Dunning wrote:
For one project, I'm required to access a web service for every page.
Basically it returns a little bit of random text. I have no control
over the web service, and there is no possibility of cacheing or
bringing it locally: the requirements are that it be accessed live
across the Internet every time.
Most times it's instantaneous, but sometimes it can hang. I want to
give it a half-second timeout, which we're allowed to do, but there's
no documentation provided. Here is how we call it now (really simple):
include('http://04planet.info/kfc/grab_one.php');
I have STFW and RTFM and could only find vague references like use
fsockopen instead or stream_set_timeout() but I can't find an
actual complete working example anywhere. Any pointers?
Generally, using include or file_get_contents() for a remote call is a
Bad Idea. Perhaps if you used curl instead? You have a lot more control
over the entire call that way. Of course, the curl code might be a bit
bulkier in PHP, but I find it works quite well.
As for fsockopen(), it works just like a file pointer.
http://www.php.net/manual/en/function.fsockopen.php
That manual page gives a pretty good description of how to use it.
Idea: The socket will by default be opened in blocking mode. You can
switch it to non-blocking mode by using stream_set_blocking().
The last parameter to fsockopen() is the timeout for connecting to the
socket, and the manual describes it as such:
Note: If you need to set a timeout for reading/writing data over the
socket, use stream_set_timeout(), as the timeout parameter to
fsockopen() only applies while connecting the socket.
Regards,
Torgny
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php