PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Dave Smith   PHP Geolocation API ipStack Wrapper   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Usage Example
Class: PHP Geolocation API ipStack Wrapper
Get the geographic location of a site with ipStack
Author: By
Last change:
Date: 6 years ago
Size: 1,567 bytes
 

Contents

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

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

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

//instantiate the class
include('ipstack.class.php');
$ips = new ipsWrapper();

//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>';
?>