views:

280

answers:

1

The website is http://www.ipalaces.org/support/

The code I use for the status indicators is

<img src="http://big.oscar.aol.com/imperialpalaces?on_url=http://www.ipalaces.org/support/widget/status_green.gif&amp;off_url=http://www.ipalaces.org/support/widget/status_offline.gif"&gt;

which is a neat thing that big.oscar.aol.com lets you do, it redirects it to whatever image you have set for the on_url if they are online, and same for off_url for offline. However, I want to use this in an if statement in PHP or javascript to display different things. Currently I am using this:

function getaim($screenname) {
     $ch  = curl_init();
     $url = "http://big.oscar.aol.com/$screenname?on_url=true&amp;off_url=false";
     curl_setopt($ch, CURLOPT_URL,$url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
     // added to fix php 5.1.6 issue:
     curl_setopt($ch, CURLOPT_HEADER, 1);
     $result = curl_exec($ch);
     curl_close($ch);

     if(eregi("true",$result)) {
      return true;
     } else {
      return false;
     }
}

If (getaim("ImperialPalaces")) { print "Online"; } else { print "Offline"; }

The problem with this code is that for some reason, at random times it can take up to 12 seconds for it to actually retrieve the results. Whereas the standard img trick is almost instant.

Is there a known issue with curl? Is there a faster way?

I've seen someone try to read the .src of the img tag and do an if statement like that, but I couldnt get it to work.

+2  A: 

To avoid waiting for a dozen seconds when things are not doing OK, you can set a couple more options, like (see curl_setopt) :

  • CURLOPT_CONNECTTIMEOUT : The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
    • setting it to a couple of seconds would be enough
  • CURLOPT_TIMEOUT : The maximum number of seconds to allow cURL functions to execute.
    • same for this one
  • CURLOPT_DNS_CACHE_TIMEOUT : The number of seconds to keep DNS entries in memory. This option is set to 120 (2 minutes) by default.
    • You could probably set this to a higher value

If users of your website generally stay on it for more than only one or two pages, it might be interesting to store that information in $_SESSION, and only fetch it once in a while.

For instance, you could fetch it only if the value stored in session has been fetched more than 5 minutes ago. It would probably save a couple of calls :-)


One other way might be to do that on the client-side :

  • fetch the image with the <img> tag
  • in case of "online", use an image that loads OK
    • plug a handler on the "load" even of the image, to replace it with some text
  • in case of "offline", use an image that is in 404
    • plug a handler on the "error" even of the image, to replace it with some text

That's not very nice (it's kind of "hacky", in the wrong way), but it should work ;-)

Your image would be like this :

<div id="arround-1">
    <img id="img-1" src="http://big.oscar.aol.com/imperialpalaces?on_url=http://www.ipalaces.org/support/widget/status_green.gif&amp;amp;off_url=http://this.is-a-404-error.com"
        onload="replace_img_status(1, 1);"
        onerror="replace_img_status(1, 0);"
    />
</div>

You see that, if the user is connected, the <img> finally leads to an image that exists ; so, the "load" even will be fired.

And, in the case the user is not connected, the <img> will finally lead to an image that doesn't exist (it's giving a 404 error) ; so, the "error" event will be fired.

You now have to take care of those two cases, with something like this :

<script type="text/javascript">
    var replace_img_status = function (num, status) {
        var div = document.getElementById('arround-' + num);
        if (div) {
            if (status == 1) {
                div.innerHTML = 'Online';
            } else {
                div.innerHTML = 'Offline';
            }
        }
    };
</script>

If status is 1, we display "Online", and, in the other case ("error"), we display "Offline" :-)

But, even is it seems to be working, I don't really like that solution ^^

Pascal MARTIN