views:

37

answers:

3

Hi there,

I have been trying to strip information out of a dynamically generated page and then insert the information into a database, this is only a one time thing. to explain the layout:

I have a list of 700 IP addresses in my database currently but they have no location linked to them, I am using the following code to retrieve the information from hostip.info and insert the returned data into a variable on a loop:


dbconnect();
$tbl_name = 'Votes';
$sql = "SELECT * FROM $tbl_name LIMIT 10"; //THIS LIMIT IS ONLY A TEMPORARY //
$result=mysql_query($sql);  
$rows=mysql_fetch_array($result);
while($rows=mysql_fetch_array($result)) {
     echo $rows['UIP']; //This is the IP stored currently
     $file_l = file_get_contents('http://api.hostip.info/get_html.php?ip='.$rows['UIP'].'&position=true');
}

For each IP tested the information returned from hostip.info looks a bit like this:

Country: UNITED KINGDOM (GB)
City: (Unknown city)
Latitude: 
Longitude: 
IP: 79.170.40.245

just in plain text, I basically want to grab the Lat, Long and City from it and whack it in a database, HOWEVER, the city, lat and long are all different lengths. Is there any way to strip this information with maybe preg_match or something like that, I have tried but Can't figure it out. Any ideas greatly appreciated.

A: 

You should use this URL instead :

http://api.hostip.info/?ip=$rows['UIP']

It returns XML which is easier to parse. You can even do it in AJAX this way \o/

Source

Chouchenos
I am such a massive spastic. Cheers. I should've thought of trying the XML.
Dave Robilliard
A: 

"; print_r($arr); ?>

srilu
A: 

No need to bother with an XML parser for something so simple.

while($rows=mysql_fetch_array($result)) 
{
    echo $rows['UIP']; //This is the IP stored currently
    $file_l = file_get_contents('http://api.hostip.info/get_html.php?ip='.$rows['UIP'].'&position=true');

    $lines = explode("\n", $file_l);
    $values = array();
    foreach($lines as $line)
    {
        $s = explode(':', $line);
        $values[$s[0]] = trim($s[1]);
    }
    doSomethingWith($values['City'], $values['Latitude'], $values['Longitude']);
}
Frode
Awesome, that's exactly what I needed, cheers for all of your input
Dave Robilliard