Home
Articles
Forums
Resources
Downloads
Search
Last Visit
IP To Country
Get Album Art
Embed Codes
Music Playlists
Music Search
Music Players
Member Links
Random Pic
Support The Cause
Who's Online

An example of extracting data from IP To Country using PHP.



News: November 19, 2008, 04:32:50 PM
Welcome, Guest. Please login or register.
Did you miss your activation email?
*

An example of extracting data from IP To Country using PHP.
Pages: [1]
  Print  

  An example of extracting data from IP To Country using PHP.
Author Message
Website Manager
Administrator
Newbie
*****
Offline Offline

Posts: 21



« on: January 06, 2008, 09:35:45 PM »

I put together an example php script that reads and extracts data from IP To Country so it can be utilized however you wish.
Code:
<?php

// Your comma-separated list of ip addresses or a single ip
$ip_list "74.53.231.66,210.213.161.50,219.165.209.190,24.55.94.84";

// The webpage to query
$the_source_page "http://jasonlau.biz/ip2country/$ip_list";

if(!
$get_source_page = @file($the_source_page))
{
  
  
$error "yes";
  echo 
"An error occurred at line 8 and the request was terminated.\n";
  exit;
  
} else {
// Get the HTML to a string  
  
$source_page_html implode(''$get_source_page);
// Narrow down the HTML to only the section we need  
  
$desired_section explode("IP 2 Country Output",$source_page_html);
// If no desired section is found return error  
if(empty($desired_section[1]))
{
  
  
$error "yes";
  echo 
"An error occurred at line 18 and the request was terminated.\n";
  exit;
  
} else {
// Get the unordered lists from the HTML  
  
$lists explode("<ul",$desired_section[1]);
// Get the list items from the lists  
  
$items explode("<li>",$lists[1]);
array_shift($items);
  foreach(
$items as $info){
// Use the comma as the key for each item part
   
$parts explode(",",$info);

   
$original_ip $parts[0];
   
$ipv4 $parts[1];
   
$country_code1 $parts[2];
   
$country_code2 $parts[3];
   
$country_name1 $parts[4];
   
$country_name2 $parts[5];
   
$country_name3 $parts[6];
   
$flag_url $parts[7];
   
$flag_html $parts[8];

/*
// just a thought -
// print the parts for each item
foreach($parts as $part){
     echo "$part<br>\n";
}
*/

  
echo "IP: $original_ip<br>\n";
  echo 
"IPv4: $ipv4<br>\n";
  echo 
"CC 1: $country_code1<br>\n";
  echo 
"CC 2:$country_code2<br>\n";
  echo 
"CN 1: $country_name1<br>\n";
  echo 
"CN 2 $country_name2<br>\n";
  echo 
"CN 3 $country_name3<br>\n";
  echo 
"Flag URL: $flag_url<br>\n";
  echo 
"Flag: $flag_html<br><br>\n";
    
  }  
 }     
}
?>

I've attached a text file to this post that contains this code. Also, I have thrown in a few test IP addresses for the demonstration.
« Last Edit: January 07, 2008, 10:35:43 AM by Website Manager » Logged

Pages: [1]
  Print  
 

Jump to: