Importing IP2Proxy data into Oracle Database and querying with PHP (IPv4)

The aim of this guide is to demonstrate how to import IP2Proxy data (PX10) in CSV form into Oracle Database and then query the data in a PHP web page.

Download commercial version at https://ip2location.com/download?code=PX10

Extract out the IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL.CSV file from the downloaded zipped file.

Important Note

We will not cover installation of Oracle or PHP in this guide. We will assume you have already setup Oracle and PHP on the localhost and are using PHP via Apache (also on the localhost). For this example, we are using a Windows 10 machine.

Importing the CSV data into Oracle

Run the following SQL in Oracle to create the table and import the data from the CSV file:

CREATE TABLE ip2proxy_px8(
  ip_from NUMBER(10) CHECK (ip_from >= 0),
  ip_to NUMBER(10) CHECK (ip_to >= 0),
  proxy_type VARCHAR2(3),
  country_code CHAR(2),
  country_name VARCHAR2(64),
  region_name VARCHAR2(128),
  city_name VARCHAR2(128),
  isp VARCHAR2(256),
  domain VARCHAR2(128),
  usage_type VARCHAR2(11),
  asn VARCHAR2(6),
  "as" VARCHAR2(256),
  last_seen NUMBER(10),
  threat VARCHAR2(128), 
  CONSTRAINT pk_ip_from_ip_to PRIMARY KEY (ip_from, ip_to)
);

CREATE OR REPLACE DIRECTORY ip_tmp AS 'C:\your_CSV_file_folder';
GRANT READ, WRITE ON DIRECTORY ip_tmp TO sys;

INSERT INTO ip2proxy_px10(ip_from, ip_to, proxy_type, country_code, country_name, region_name, city_name, isp, domain, usage_type, asn, "as", last_seen, threat)
SELECT ip_from2, ip_to2, proxy_type2, country_code2, country_name2, region_name2, city_name2, isp2, domain2, usage_type2, asn2, as2, last_seen2, threat2
FROM EXTERNAL (
  (ip_from2 NUMBER(10),
  ip_to2 NUMBER(10),
  proxy_type2 VARCHAR2(3),
  country_code2 CHAR(2),
  country_name2 VARCHAR2(64),
  region_name2 VARCHAR2(128),
  city_name2 VARCHAR2(128),
  isp2 VARCHAR2(256),
  domain2 VARCHAR2(128),
  usage_type2 VARCHAR2(11),
  asn2 VARCHAR2(6),
  as2 VARCHAR2(256),
  last_seen2 NUMBER(10)
  threat2 VARCHAR2(128))
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ip_tmp
ACCESS PARAMETERS (
  RECORDS DELIMITED BY 0x'0A'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
)
LOCATION ('IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL.CSV') REJECT LIMIT 0) px10_external;

EXEC DBMS_STATS.GATHER_TABLE_STATS('sys','ip2proxy_px10');

Querying the data in PHP

Now, create a PHP file called test.php in your website.

Paste the following PHP code into it and then run it in the browser:

<?php
$ip = '8.8.8.8';

function queryIP2Proxy($myip) {
  $user = 'sys';
  $pass = '12345';
  $host = 'localhost';
  $port = 1521;
  $service = 'XE';
  $tns = '
  (DESCRIPTION =
    (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = ' . $host . ')(PORT = ' . $port . '))
    )
    (CONNECT_DATA =
    (SERVICE_NAME = ' . $service . ')
    )
  )';
  
  if ($conn = oci_connect($user, $pass, $tns, 'AL32UTF8', OCI_SYSDBA)) {
    $myipnum = sprintf('%u', ip2long($myip));
    
    $stid = oci_parse($conn, 'SELECT * FROM ip2proxy_px10 WHERE :ip_num >= ip_from AND :ip_num <= ip_to ORDER BY ip_from, ip_to FETCH FIRST 1 ROWS ONLY');
    oci_bind_by_name($stid, ':ip_num', $myipnum);
    
    oci_execute($stid);
    $num = oci_fetch_all($stid, $res, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
    
    oci_close($conn);
    
    if ($num > 0) {
      return $res[0];
    }
    else {
      die('No record found.' . "<br>\n");
    }
  }
  else {
    $err = oci_error();
    die('Error: ' . $err['message'] . "<br>\n");
  }
}

$myresult = queryIP2Proxy($ip);

echo 'PROXY_TYPE: ' . $myresult["PROXY_TYPE"] . "<br>\n";
echo 'COUNTRY_CODE: ' . $myresult["COUNTRY_CODE"] . "<br>\n";
echo 'COUNTRY_NAME: ' . $myresult["COUNTRY_NAME"] . "<br>\n";
echo 'REGION_NAME: ' . $myresult["REGION_NAME"] . "<br>\n";
echo 'CITY_NAME: ' . $myresult["CITY_NAME"] . "<br>\n";
echo 'ISP: ' . $myresult["ISP"] . "<br>\n";
echo 'DOMAIN: ' . $myresult["DOMAIN"] . "<br>\n";
echo 'USAGE_TYPE: ' . $myresult["USAGE_TYPE"] . "<br>\n";
echo 'ASN: ' . $myresult["ASN"] . "<br>\n";
echo 'AS: ' . $myresult["as"] . "<br>\n"; // this is lowercase due to the name being a reserved keyword
echo 'LAST_SEEN: ' . $myresult["LAST_SEEN"] . "<br>\n";
echo 'THREAT: ' . $myresult["THREAT"] . "<br>\n";
?>

 

Was this article helpful?

Related Articles