PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Dave Smith   PHP Geo Location by Internet Protocol API   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example Usage
Class: PHP Geo Location by Internet Protocol API
Find the geographical location by remote IP Number
Author: By
Last change:
Date: 5 years ago
Size: 1,561 bytes
 

Contents

Class file image Download
<?php
/*
example usage
ipapiWrapper ver 1.0

You must get an API key from https://ipapi.com/product
and enter it in the ipapi.class.php file
*/

//turning off low level notices
error_reporting(E_ALL ^ E_NOTICE);

//instantiate the class
include('ipapi.class.php');
$ips = new ipapiWrapper();

//set our endpoint
//defaults to the api endpoint, but we will set it just to be safe
$ips->setEndPoint('api');

//set the ip number to be checked
$ips->ipnum = '134.201.250.155';

//get the response from the api
$ips->getResponse();

//the reponse property will contain the response returned from the api
echo '<h4>API response for ip: '.$ips->ipnum.'</h4>';
echo
'Country: '.$ips->response->country_name.'<br>';
echo
'Region/State: '.$ips->response->region_name.'<br>';
echo
'City: '.$ips->response->city.'<br>';
echo
'Postal Code: '.$ips->response->zip.'<br>';

//full response
echo '<hr>';
echo
'<h4>Full response</h4>';
echo
'<pre>';
var_dump($ips->response);
echo
'</pre>';

/*
The check endpoint uses the ip number that sent the request
*/

//reset the params
$ips->resetParams();

//set the endpoint to check
$ips->setEndPoint('check');

//request only the main data for the ip number using the fields param
$ips->setParam('fields','main');

//get the response from the api
$ips->getResponse();

//the reponse property will contain the response returned from the api
echo '<h4>API response for originating ip: '.$ips->response->ip.'</h4>';
echo
'<pre>';
var_dump($ips->response);
echo
'</pre>';
?>