views:

95

answers:

2

I am working on the development of a web based application on codeigniter. I wrote this function to get the timezone data for any city or address.

function gettimezone($address)
    {
       $_url = 'http://api.local.yahoo.com/MapsService/V1/geocode';
        $_url .= sprintf('?appid=%s&location=%s',"phpclasses",rawurlencode($address));
        $_result = false;
        if($_result = file_get_contents($_url)) {
            preg_match('!<Latitude>(.*)</Latitude><Longitude>(.*)</Longitude>!U', $_result, $_match);
            $lng = $_match[2];
            $lat = $_match[1]; 
            $url = "http://ws.geonames.org/timezone?lat={$lat}&amp;lng={$lng}";
            $timedata = file_get_contents($url);
            $sxml = simplexml_load_string($timedata);
            return $sxml->timezone;
        } 
        else
            return false;
    }

I am working on Windows 7 and xampp 1.7 (php 5.2.8).
Is there any possible scenario where this piece of code would crash Apache? The application runs fine almost everytime, but I think running this piece of code when there is no internet connectivity or very poor internet connectivity (thereby no access to either of the webservices I have used) brings down the server. Something like this happened a few times and I think it was due to loss of internet connectivity but I am not sure what exactly is going on.

[ By the way, feel free to use this piece of code. Makes 2 web service calls so don't use to too often in your application. Consider storing the results in a database.]

Edit: This piece of code and my application works perfectly, almost every time. It just crashed Apache a couple of times a few days ago. And I think its this function is the culprit as this was one of the things that was added just before those crashes.

Edit 2: Here is a small snippet of my apache error log that doesn't look right. This is there in the log a few times, and I think(quite sure actually) it corresponds to the server crash. Apart from this, there are a few "file does not exist" errors, which won't crash the server

[Mon May 24 12:14:26 2010] [notice] Parent: child process exited with status 3221225477 -- Restarting.
[Mon May 24 12:15:29 2010] [warn] pid file c:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
A: 

Apart from absence of internet connectivity, it won't work if the allow_url_fopen is set to false from php.ini file and for this reason, file_get_contents function won't fetch data from a url.

Sarfraz
No.. that is enabled. See, the real tricky thing about this situation is that this code and the application works almost every single time. Those crashes just happened a few times, but I still need to investigate as I can't bring the web server down. Something tells me its this piece of code, as Apache never used to crash before I added this functionality.
Munim
@Munim: Reading external data might be the reason, sometimes you don't get it because of various reasons, put a check condition for this line too eg: `if ($timedata = file_get_contents($url);){// further code};`
Sarfraz
@Sarfaz Oh.. I don't know why I missed that. Hopefully it will help. Thanks. @Anyone else: Please see if there is anything else that could be wrong with this piece of code.
Munim
+1  A: 

Since there is no Error StackTarace or error log, I suspect the cause of the problem might be:

 $url = "http://ws.geonames.org/timezone?lat={$lat}&amp;lng={$lng}";

Because you are trying to get somedata from another web service, so this might be the problem.

I would say, put a try/catch around that section like this:

try
{
    $url = "http://ws.geonames.org/timezone?lat={$lat}&amp;lng={$lng}";
    $timedata = file_get_contents($url);
    $sxml = simplexml_load_string($timedata);
}
catch(Exception $e)
{
    // log this exception to file
}
return $sxml->timezone;

Like you said, it works most of the time. So leave it now and after a week or so, come back and see if the error log contains something interesting for you.

Michael Mao
@Michael I will add the exception handling routine just in case. Thanks for your suggestion. Can you look at my latest edit and see if those errors in the log make any sense to you?
Munim
@Munim : Ahhh, Google tells me this is due to the improper configuration of xampp. check http://www.java-samples.com/showtutorial.php?tutorialid=1050 for a reference. So I would say this is not a problem caused by your code probably. Fix the xampp environment might resolve this issue once and for all. Also, thatexplains why your actual server never reports any crash :)
Michael Mao
@Micheal I did this too. Not very sure if this could be the problem though. Because these crashes are very rare. xampp works fine almost everytime.
Munim