Coder Social home page Coder Social logo

ipinfo's Introduction

ipinfo

Latest version Build Status Coverage Status Quality Score Total Downloads PSR2 Conformance

A wrapper around the ipinfo.io services.

Install

You can install the library using composer:

$ composer require davidepastore/ipinfo

How to use

Settings

Token

You can set your token when you instantiate the object but it's not mandatory.

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

cURL options

The cURL options to use while trying to connect when you instantiate the object:

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

Read details about the given ip

You can read all the properties from the given ip.

//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);

Read details about your ip

You can read all the properties from your ip.

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

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

Get info from the host

After obtaining the Host instance you can read all the properties or each of them individually.

//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();

Read only a field

There are different constants that you could use to read specific field value from an Ipinfo instance using the getSpecificField() and getYourOwnIpSpecificField() methods:

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

Read only the Geo data (which is faster)

By using the getIpGeoDetails() method you will get less fields. This call tends to be faster than getFullIpDetails() so use this call in case you only need the following fields:

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

These fields will be empty:

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

Error Handling

You can handle all the types of IpInfo exceptions by catching the IpInfoExceptionException:

use DavidePastore\Ipinfo\Exception\IpInfoExceptionException;

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

Invalid Token Exception

It could happen that the token you are using to make the API call is not valid. You can handle it by catching the InvalidTokenException:

use DavidePastore\Ipinfo\Exception\InvalidTokenException;

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

Rate Limit Exceed Exception

It could happen that your API call exceeds the rate limit. You can handle it by catching the RateLimitExceedException:

use DavidePastore\Ipinfo\Exception\RateLimitExceedException;

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

Wrong Ip Exception

It could happen that your API call is trying to obtain info about a wrong ip. You can handle it by catching the WrongIpException:

use DavidePastore\Ipinfo\Exception\WrongIpException;

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

Issues

If you have issues, just open one here.

ipinfo's People

Contributors

cmwelsh avatar davidepastore avatar jamesfairhurst avatar rvalitov avatar rvanlaak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ipinfo's Issues

Few issues

Hi!
Thank you for your work, but current version lacks the following functionality:

  • use HTTPS to contact ipinfo.io instead of HTTP
  • set timeout for the operation

If you could improve that, then it could be a great project!

Update installation docs

May be it's better to change the way the lib is installed, and do it like this, so it's safe to use the latest release of the same major version:

"require" : {
	"davidepastore/ipinfo" : "^0.3.*"
}

Throw exception if API call fails

I'm currently getting this error when the API returns unsuccessful response... no way to catch these in my application.

E_WARNING: array_merge(): Argument #2 is not an array
1 File "/public_html/vendor/davidepastore/ipinfo/src/DavidePastore/Ipinfo/Host.php" line 33 in array_merge
2 File "/public_html/vendor/davidepastore/ipinfo/src/DavidePastore/Ipinfo/Host.php" line 33 in __construct
3 File "/public_html/vendor/davidepastore/ipinfo/src/DavidePastore/Ipinfo/Ipinfo.php" line 153 in checkGeo
4 File "/public_html/vendor/davidepastore/ipinfo/src/DavidePastore/Ipinfo/Ipinfo.php" line 125 in getSpecificField

results are null

Why my results are null from the $host variable

The code i have used is:

require DIR . '/vendor/autoload.php';

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

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

My result is:
DavidePastore\Ipinfo\Host Object ( [properties:protected] => Array ( [city] => [country] => [hostname] => [ip] => [loc] => [org] => [phone] => [postal] => [region] => ) )

String response is not handled properly

$response = $this->jsonDecodeResponse($response);

In case there is a string response from ipinfo, the json decoding function returns a NULL. This in turn fails in the Host.php constructor.

Scenario: When subscription has expired. The below response is received,
Rate limit exceeded. Subscribe to a paid plan to increase your usage limits at http://ipinfo.io/pricing, or contact us via http://ipinfo.io/contact

As explained above, this fails as array_merge in the Host.php constructor is called with a NULL input.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.