[PHP SDK] IP geolocation, proxy detection and WHOIS lookup
Tags: php, programming, api, webdev
Published: 2023-05-23 07:48:26

The IP2Location IO PHP SDK is an all-in-one solution that encapsulates the implementation of IP2Location, IP2Proxy, and IP2WHOIS services into a unified SDK.

This IP2Location IO PHP SDK bundled with pre-built libraries of IP address lookup services, such as geolocation data lookup, proxy detection, and WHOIS information that help developers streamline development. This ready-to-use modules is designed to be developer-friendly that accelerate development and helps developers to reduce the amount of code when integrating the SDK into their projects. 


Of course, some of the developers may still prefer to to write from scratch or use the REST API directly. In this case, you may visit IP2Location.io website to check on the [REST API documentation](https://www.ip2location.io/ip2location-documentation#devto). You are free to choose which approach best suits your needs.

Below I am going to show you the steps to install the SDK and perform the API calling in a PHP page. To start, please make sure you have PHP and Composer pre-installed in your environment. I will skip the steps to install PHP and Composer, but you may visit the respective sites to learn more.

## Step 1: Install IP2Location IO PHP SDK

At your project root directory, run the below command:

```
composer require ip2location/ip2location-io-php
```

The above command will install IP2Location IO PHP into your project. If you have not installed any modules previously using Composer, you should notice that the composer.json, composer.lock, and a vendor folder will be created in your directory.

## Step 2: Create a PHP test file and write the codes

Create a test file named test.php. Open the file with your preferred text editor, and write the below codes:

```
<?php
 require "vendor/autoload.php";
 
 $config = new \IP2LocationIO\Configuration('YOUR_API_KEY');
 $ip2locationio = new IP2LocationIO\IPGeolocation($config);
 
 // Lookup ip address geolocation data
 try{
            $result = $ip2locationio->lookup('8.8.8.8');
            print_r($result);
 }
 catch (Exception $e){
            die ("Error found: " . $e->getMessage());
 }
```

The IP2Location IO requires an API key to work. You may sign up for a [free plan](https://www.ip2location.io/pricing#devto) and see the difference it can make in your workflow.


What the above code did is to instantiate an IP2LocationIO object that you could use for the geolocation lookup. In the above example, the input IP address is 8.8.8.8. Look at the result below.

```
stdClass Object
(
    [ip] => 8.8.8.8
    [country_code] => US
    [country_name] => United States of America
    [region_name] => California
    [city_name] => Mountain View
    [latitude] => 37.405992
    [longitude] => -122.078515
    [zip_code] => 94043
    [time_zone] => -08:00
    [asn] => 15169
    [as] => Google LLC
    [is_proxy] =>
) 
```

The structure of the returned object depend on your subscribed plan. The example above is for the Free plan. If you subscribe to a higher plan, you will receive more detailed information, such as usage type, mobile data, ISP, domain, district, ASN, and more.

As shown in the above example, IP2Location and IP2Proxy information are returned together in a single lookup call, which helps to minimize coding overhead and reduces the need to make multiple calls. To check if a given IP address is a proxy, simply look at the “is_proxy” field, which will return either “True” or “False”. If you require additional information on the proxy, such as its type, provider, and so on, you may need to subscribe to a higher plan.

In addition to the IP2Location and IP2Proxy information, this SDK also includes the ability to query IP2WHOIS data. The IP2WHOIS domain lookup is also available in the Free plan for up to 500 lookups per month. Let’s look at the example of an IP2WHOIS domain lookup below:

```
$domainwhois = new IP2LocationIO\DomainWhois($config);

 // Lookup domain information
 try{
  $result = $domainwhois->lookup('locaproxy.com');
  print_r($result);
 }
 catch (Exception $e){
  die ("Error found: " . $e->getMessage());
 }
```

The above code will instantiate a DomainWhois object and later will lookup for the domain information of locaproxy.com. Below is the example of the results returned.

```
stdClass Object
(
    [domain] => locaproxy.com
    [domain_id] => 1710914405_DOMAIN_COM-VRSN
    [status] => clientTransferProhibited <https://icann.org/epp#clientTransferProhibited>
    [create_date] => 2012-04-03T02:34:32Z
    [update_date] => 2021-12-03T02:54:57Z
    [expire_date] => 2024-04-03T02:34:32Z
    [domain_age] => 4014
    [whois_server] => whois.godaddy.com
    [registrar] => stdClass Object
        (
            [iana_id] => 146
            [name] => GoDaddy.com, LLC
            [url] => <https://www.godaddy.com>
        )

    [registrant] => stdClass Object
        (
            [name] => Registration Private
            [organization] => Domains By Proxy, LLC
            [street_address] => DomainsByProxy.com
            [city] => Tempe
            [region] => Arizona
            [zip_code] => 85284
            [country] => US
            [phone] => +1.4806242599
            [fax] => +1.4806242598
            [email] => Select Contact Domain Holder link at <https://www.godaddy.com/whois/results.aspx?domain=LOCAPROXY.COM>
        )

    [admin] => stdClass Object
        (
            [name] => Registration Private
            [organization] => Domains By Proxy, LLC
            [street_address] => DomainsByProxy.com
            [city] => Tempe
            [region] => Arizona
            [zip_code] => 85284
            [country] => US
            [phone] => +1.4806242599
            [fax] => +1.4806242598
            [email] => Select Contact Domain Holder link at <https://www.godaddy.com/whois/results.aspx?domain=LOCAPROXY.COM>
        )

    [tech] => stdClass Object
        (
            [name] => Registration Private
            [organization] => Domains By Proxy, LLC
            [street_address] => DomainsByProxy.com
            [city] => Tempe
            [region] => Arizona
            [zip_code] => 85284
            [country] => US
            [phone] => +1.4806242599
            [fax] => +1.4806242598
            [email] => Select Contact Domain Holder link at <https://www.godaddy.com/whois/results.aspx?domain=LOCAPROXY.COM>
        )

    [billing] => stdClass Object
        (
            [name] =>
            [organization] =>
            [street_address] =>
            [city] =>
            [region] =>
            [zip_code] =>
            [country] =>
            [phone] =>
            [fax] =>
            [email] =>
        )

    [nameservers] => Array
        (
            [0] => vera.ns.cloudflare.com
            [1] => walt.ns.cloudflare.com
        )

)

```
In addition to the above lookup function, the SDK also provide several useful functions such as
- Convert Normal Text to Punycode
- Convert Punycode to Normal Text
- Get Domain Name from an URL
- Get Domain Extension from an URL


Overall, the IP2Location IO PHP SDK provides a comprehensive solution for IP address lookup, geolocation, proxy detection, and WHOIS domain information. With this unified SDK, developers can easily integrate IP2Location IP geolocation services into their projects, and improve their efficiency and productivity. The SDK is available on Composer, making it easy to install and use. Additionally, the IP2Location IO website provides comprehensive documentation for the REST API to facilitate integration into projects, so developers can choose which approach best suits their needs.