PHP Classes

PHP Geo Location: Get geographic details of a world location by name

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 270 All time: 7,720 This week: 61Up
Version License PHP version Categories
geo-location 1.0.0GNU General Publi...5PHP 5, Web services, Geography
Description 

Author

This class can get geographic details of a world location by name.

It can send a request to the City Geo-Location Lookup API Mashape Web server to search for locations with a given name.

The class returns the details of one or more locations found with the given name in an array or JSON string like the location full name, zip code, longitude and latitude, time zone, etc..

Picture of Samuel Adeshina
  Performance   Level  
Name: Samuel Adeshina is available for providing paid consulting. Contact Samuel Adeshina .
Classes: 19 packages by
Country: Nigeria Nigeria
Age: 25
All time rank: 5232 in Nigeria Nigeria
Week rank: 43 Up3 in Nigeria Nigeria Down
Innovation award
Innovation award
Nominee: 7x

Winner: 1x

Example

<?php
   
/*
        This package is distributed with a composer.json file.
        If you have composer installed, you have to install the contents of the file
        It creates an autoload class that contains the Unirest Class.
        For users without composer, you need to download the Unirest class provided by Mashape.
        You can find it on Github.

        After getting the necessary autoloads, you need to require the vendor/autoload.php file
        so that the Unirest class can be included in your script. Then include the geolocation.class.php
        class too.
    */

   
require_once("vendor/autoload.php");
    require_once(
"geolocation.class.php");
   
    use
Unirest\Request; //We use the Request class thats created in the Unirest namespace to make life easier later on

   
$geoLocation = new GeoLocation(new Request()); //Instantiate the GeoLocation() class, accepts a Unirest\Request Object (class) as parameter

   
$locationToSearchFor = "Abuja"; //This can be any place in the world. It can be a city, village, country, state and so on.

   
$location = $geoLocation->getLocation($locationToSearchFor); //queries the api server for the complete geo-data of the location you searched for

    /*
        You can var_dump or print_r the $location variable to get a full look at the values returned by the getLocation() method
    */
   
$matches = $geoLocation->getMatches(); //the getLocation method returns more than one result, you use this method to count the number of results returned
   
    /* We loop through all the results returned below */
   
for ($count = 0; $count < $matches; $count++)
    {
        echo
"Name: ".$geoLocation->getName($count) . "<br/>"; //The name of the current matched result
       
echo "Type: ".$geoLocation->getType($count) . "<br/>"; //The type, could be city, country, state and so on
       
echo "Country: ".$geoLocation->getCountry($count) ."<br/>"; //The country where the matched result is located
       
echo "Zip Code: ".$geoLocation->getZipCode($count) . "<br/>"; //the zip code of the location that's searched for
       
echo "Magic Code: ".$geoLocation->getMagicCode($count) . "<br/>"; //The magic code of the matched result
       
echo "WMO Identifier: ".$geoLocation->getWMOID($count). "<br/>"; //The WMO Identifier
       
echo "Time Zone: ".$geoLocation->getTimeZone($count)."<br/>"; //The current time zone of the matched result
       
echo "Standard Time Zone Abbreviation: ".$geoLocation->getStandardTimeZone($count)."<br/>"; //returns the abbreviated standard time zone
       
echo "Longitude: ".$geoLocation->getLongitude($count)."<br/>"; //returns the longitude of the location returned
       
echo "Latitude: ".$geoLocation->getLatitude($count)."<br/>"; //returns the latitude of the returned location
       
echo "<hr/>"; //an horizontal rule to separate our results
   
}


   
/* You can browse through the geolocation.class.php file to get further details about the methods and
        look at other ways you can achieve the same task. for instance you could also get the longitude of a
        place by doing this:
            echo $geoLocation->getLatLong($count)["Lon"];
    */


Details

PHP-Geo-Location

This PHP class/package can be used to get the complete geographical information about any place around the world. By providing a name of any location in the world, it returns the full geo-data of places that matches the location that was searched for. It returns details such as the full name of a place, the zip code, the longitude and latitude, the WMO Identifier and the Magic codes, the Time Zone, the standard time zones and so on. It can be very useful in applications or scenarios where other details about a users location needs to be automatically provided when the name of that location is provided.

Please go through the example.php file to fully understand how this class works.


  Files folder image Files (7)  
File Role Description
Accessible without login Plain text file composer.json Data install's Unirest via composer
Accessible without login Plain text file config.ini Data configuration file
Accessible without login Plain text file example.php Example Explains how to use the GeoLocation class by instantiating one
Plain text file geolocation.class.php Class The main class file
Plain text file geolocation.interface.php Class provided in case a serious change is made to the class file
Accessible without login Plain text file LICENSE Lic. License File
Accessible without login Plain text file README.md Data ReadMe File

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:270
This week:0
All time:7,720
This week:61Up