PHP code example of davidepastore / ipinfo

1. Go to this page and download the library: Download davidepastore/ipinfo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

davidepastore / ipinfo example snippets


$ipInfo = new DavidePastore\Ipinfo\Ipinfo(array(
	"token" => "your_api_key"
));

$ipInfo = new DavidePastore\Ipinfo\Ipinfo(array(
	"curlOptions" => array(
            CURLOPT_CONNECTTIMEOUT => 1,
            CURLOPT_TIMEOUT => 2,
            CURLOPT_CAINFO => __DIR__ . "/cacert.pem"
    )
));

//Get all the properties
$host = $ipInfo->getFullIpDetails("8.8.8.8");

//Get only a single property (this could save bandwidth)
$city = $ipInfo->getSpecificField("8.8.8.8", DavidePastore\Ipinfo\Ipinfo::CITY);

//Get all the properties
$host = $ipInfo->getYourOwnIpDetails();

//Get only a single property (this could save bandwidth)
$city = $ipInfo->getYourOwnIpSpecificField(DavidePastore\Ipinfo\Ipinfo::CITY);

//Read all the properties
$city = $host->getCity();
$country = $host->getCountry();
$hostname = $host->getHostname();
$ip = $host->getIp();
$loc = $host->getLoc();
$org = $host->getOrg();
$phone = $host->getPhone();
$postal = $host->getPostal();
$region = $host->getRegion();

//Get the associative array with all the properties
$properties = $host->getProperties();

IpInfo::IP; //For the ip address
IpInfo::HOSTNAME; //For the hostname
IpInfo::LOC; //For the loc
IpInfo::ORG; //For the org
IpInfo::CITY; //For the city
IpInfo::REGION; //For the region
IpInfo::COUNTRY; //For the country
IpInfo::PHONE; //For the phone
IpInfo::POSTAL; //For the postal
IpInfo::GEO; //For the geo info. See the paragraph below for more info

IpInfo::IP; //For the ip address
IpInfo::CITY; //For the city
IpInfo::REGION; //For the region
IpInfo::COUNTRY; //For the country
IpInfo::PHONE; //For the phone
IpInfo::POSTAL; //For the postal

IpInfo::HOSTNAME; //For the hostname
IpInfo::LOC; //For the loc
IpInfo::ORG; //For the org

use DavidePastore\Ipinfo\Exception\IpInfoExceptionException;

try {
    $host = $ipInfo->getFullIpDetails("8.8.8.8");
} catch (IpInfoExceptionException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

use DavidePastore\Ipinfo\Exception\InvalidTokenException;

try {
    $host = $ipInfo->getFullIpDetails("8.8.8.8");
} catch (InvalidTokenException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

use DavidePastore\Ipinfo\Exception\RateLimitExceedException;

try {
    $host = $ipInfo->getFullIpDetails("8.8.8.8");
} catch (RateLimitExceedException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

use DavidePastore\Ipinfo\Exception\WrongIpException;

try {
    $host = $ipInfo->getFullIpDetails("qwerty");
} catch (WrongIpException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}