PHP library to access the OpenCage geocoding API

Overview

OpenCage Geocoding API Library for PHP

A PHP library to use the OpenCage geocoding API.

Build Status / Code Quality

Build Status PHP version Twitter Follow

Overview

This library attempts to use the CURL extension to access the OpenCage Geocoding API. If CURL support is not available, the library falls back to using fopen wrappers.

PHP 7 and 8 are supported.

To use the library you must either have the CURL extension compiled into your version of PHP or configure the use of fopen wrappers via the allow_url_fopen directive in your php.ini.

Authentication

You need an API key, which you can sign up for here.

Installation

With Composer

The recommended - and easiest way - to install is via Composer. Require the library in your project's composer.json file.

$ composer require opencage/geocode

Import the Geocoder class.

require "vendor/autoload.php";

Start geocoding

$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
$result = $geocoder->geocode('82 Clerkenwell Road, London');
print_r($result);

The old fashioned way

See the file demo/geocode.php

Example results

Array
(
    [total_results] => 2
    [status] => Array
        (
            [message] => OK
            [code] => 200
        )
    [results] => Array
        (
            [0] => Array
                (
                    [annotations] => Array
                        (
                            [DMS] => Array
                                (
                                    [lat] => 51° 31' 21.60894'' N
                                    [lng] => 0° 6' 8.95198'' E
                                )

                            [MGRS] => 30UYC0100511930
                            [Maidenhead] => IO91wm75qk
                            [Mercator] => Array
                                (
                                    [x] => -11408.763
                                    [y] => 6680801.955
                                )

                            [OSGB] => Array
                                (
                                    [easting] => 531628.199
                                    [gridref] => TQ 316 821
                                    [northing] => 182177.015
                                )

                            [OSM] => Array
                                (
                                    [url] => http://www.openstreetmap.org/?mlat=51.52267&mlon=-0.10249#map=17/51.52267/-0.10249
                                )

                            [callingcode] => 44
                            [geohash] => gcpvjemm7csmhczg9cvt
                            [sun] => Array
                                (
                                    [rise] => Array
                                        (
                                            [apparent] => 1452931140
                                            [astronomical] => 1452923940
                                            [civil] => 1452928800
                                            [nautical] => 1452926280
                                        )

                                    [set] => Array
                                        (
                                            [apparent] => 1452961320
                                            [astronomical] => 1452968520
                                            [civil] => 1452963660
                                            [nautical] => 1452966120
                                        )

                                )

                            [timezone] => Array
                                (
                                    [name] => Europe/London
                                    [now_in_dst] => 0
                                    [offset_sec] => 0
                                    [offset_string] => 0
                                    [short_name] => GMT
                                )

                            [what3words] => Array
                                (
                                    [words] => gallons.trim.tips
                                )

                        )

                    [bounds] => Array
                        (
                            [northeast] => Array
                                (
                                    [lat] => 51.5227563
                                    [lng] => -0.1023801
                                )

                            [southwest] => Array
                                (
                                    [lat] => 51.5226042
                                    [lng] => -0.1025907
                                )

                        )

                    [components] => Array
                        (
                            [city] => London
                            [country] => United Kingdom
                            [country_code] => gb
                            [house_number] => 82
                            [postcode] => EC1M 5RF
                            [road] => Clerkenwell Road
                            [state] => England
                            [state_district] => Greater London
                            [suburb] => Clerkenwell
                        )

                    [confidence] => 10
                    [formatted] => 82 Clerkenwell Road, London EC1M 5RF, United Kingdom
                    [geometry] => Array
                        (
                            [lat] => 51.52266915
                            [lng] => -0.10248666188363
                        )

                )

            [1] => Array
                (
                    [annotations] => Array
                        (
                            [DMS] => Array
                                (
                                    [lat] => 51° 30' 30.70800'' N
                                    [lng] => 0° 7' 32.66400'' E
                                )

                            [MGRS] => 30UXC9945410295
                            [Maidenhead] => IO91wm42vb
                            [Mercator] => Array
                                (
                                    [x] => -13997.313
                                    [y] => 6678279.278
                                )

                            [OSGB] => Array
                                (
                                    [easting] => 530055.544
                                    [gridref] => TQ 300 805
                                    [northing] => 180563.298
                                )

                            [OSM] => Array
                                (
                                    [url] => http://www.openstreetmap.org/?mlat=51.50853&mlon=-0.12574#map=17/51.50853/-0.12574
                                )

                            [geohash] => gcpvj0u6yjcmwxz8bn43
                            [sun] => Array
                                (
                                    [rise] => Array
                                        (
                                            [apparent] => 1452931140
                                            [astronomical] => 1452923940
                                            [civil] => 1452928800
                                            [nautical] => 1452926340
                                        )

                                    [set] => Array
                                        (
                                            [apparent] => 1452961320
                                            [astronomical] => 1452968520
                                            [civil] => 1452963660
                                            [nautical] => 1452966120
                                        )

                                )

                            [timezone] => Array
                                (
                                    [name] => Europe/London
                                    [now_in_dst] => 0
                                    [offset_sec] => 0
                                    [offset_string] => 0
                                    [short_name] => GMT
                                )

                            [what3words] => Array
                                (
                                    [words] => thing.then.link
                                )

                        )

                    [bounds] => Array
                        (
                            [northeast] => Array
                                (
                                    [lat] => 51.7202301025
                                    [lng] => 0.336111992598
                                )

                            [southwest] => Array
                                (
                                    [lat] => 51.2786598206
                                    [lng] => -0.523222982883
                                )

                        )

                    [components] => Array
                        (
                            [country] => United Kingdom
                            [county] => Greater London
                            [state] => England
                            [town] => London
                        )

                    [confidence] => 1
                    [formatted] => London, Greater London, United Kingdom
                    [geometry] => Array
                        (
                            [lat] => 51.50853
                            [lng] => -0.12574
                        )

                )

        )
)

Copyright

Copyright (c) OpenCage GmbH. See LICENSE for details.

Who is OpenCage GmbH?

We run the OpenCage Geocoder. Learn more about us.

We also run Geomob, a series of regular meetups for location based service creators, where we do our best to highlight geoinnovation. If you like geo stuff, you will probably enjoy the Geomob podcast.

-- end --

Comments
  • Geocoder returning empty results.

    Geocoder returning empty results.

    I have followed the example my in lumen project but i get an empty results object. use OpenCage\Geocoder\Geocoder; class UserData extends Controller{

    public function find_user(Request $request){
    
        $geocoder = new \OpenCage\Geocoder\Geocoder('******');
        $result = $geocoder->geocode('82 Clerkenwell Road, London');
        return response()->json($result['results'][0]);
    }
    

    }

    opened by TAUFEEQ1 4
  • Added request optional parameter functionality

    Added request optional parameter functionality

    There was no possibility to add Optional Parameters: https://geocoder.opencagedata.com/api#forward-opt

    Now they can be set as array:

    $geocoder = new \OpenCage\Geocoder\Geocoder('API-KEY'); $result = $geocoder->geocode("Brivibas street 1, Riga",['countrycode'=>'lv','min_confidence'=>8]);

    opened by edgarsstrods 1
  • Update tests with new Ubuntu, PHP 8, phpunit 9

    Update tests with new Ubuntu, PHP 8, phpunit 9

    Version 3.0 as PHP 5.6 supports ends. (PHP 5.x is EOL two years ago https://www.php.net/eol.php)

    Tested against PHP 8. Upgraded phpunit and use Ubuntu 20 instead of 18 as test environment.

    opened by mtmail 0
  • PHPUnit 7, fix PHPCS issues

    PHPUnit 7, fix PHPCS issues

    We still support PHP 5.6 but the development setup moves to PHP 7.1 to use a modern (supported) version of PHPUnit (https://phpunit.de/supported-versions.html).

    opened by mtmail 0
  • version 2.0

    version 2.0

    • code cleanup and added PHP linter (code sniffer)
    • namespacing
    • composer works with autoloading
    • PHP Unit test suite
    • Travis-CI tests using live (encrypted) API key
    • new README

    Installation instructions are somewhat based on https://github.com/what3words/w3w-php-wrapper (thx @vicchi)

    opened by mtmail 0
Owner
OpenCage GmbH
easy, open, affordable, worldwide geocoding API
OpenCage GmbH
ESRI ShapeFile library for PHP

shapefile ShapeFile library for PHP Features Currently the 2D and 3D variants except MultiPatch of the ShapeFile format as defined in https://www.esri

phpMyAdmin 23 Jun 29, 2022
The most featured Geocoder library written in PHP.

Geocoder Important: You are browsing the documentation of Geocoder 4.x. Documentation for version 3.x is available here: Geocoder 3.x documentation. D

Geocoder 3.9k Jan 2, 2023
Simple Yet Powerful Geo Library for PHP

phpgeo - A Simple Geo Library for PHP phpgeo provides abstractions to geographical coordinates (including support for different ellipsoids) and allows

Marcus Jaschen 1.4k Dec 27, 2022
Geo-related tools PHP 5.4+ library built atop Geocoder and React libraries

Geotools Geotools is a PHP geo-related library, built atop Geocoder and React libraries. Features Batch geocode & reverse geocoding request(s) in seri

The League of Extraordinary Packages 1.3k Dec 27, 2022
PHP library for determining the physical location of binaries

Bin Locator Library for searching binary files in the operating system. Requirements PHP >= 7.4 Installation Library is available as composer reposito

PHP FFI 8 Jul 27, 2022
PHP library to easily get static image from French Cadastral Government map with markers and lines.

PHP French Cadastral Map Static API PHP library to easily get static image from French Cadastral Government map with markers and lines. Map source : c

Franck Alary 6 Nov 29, 2022
PHP library to easily get static image from OpenStreetMap (OSM) with markers and lines.

PHP OpenStreetMap Static API PHP library to easily get static image from OpenStreetMap with markers and lines. ✨ Supporting ⭐ Star this repository to

Franck Alary 34 Jan 2, 2023
Official PHP library for IPinfo (IP geolocation and other types of IP data)

This is the official PHP client library for the IPinfo.io IP address API, allowing you to lookup your own IP address,

IPinfo 171 Jan 2, 2023
Simple solution for converting excel rows into coordinates using Google Maps API

Package Usage Start by creating a new instance of ExcelToCoorindates class: $excel_to_cordinates = new ExcelToCoordinates(); Methods ->setEx

Nikola Cirkovic 2 Oct 16, 2021
PHP extension for geospatial rendering with Mapnik

PHP7 Mapnik Introduction This project is an extension for PHP 7 that enables geospatial rendering with Mapnik. Create tile generation scripts, dynamic

Garrett Johnson 20 Dec 14, 2022
PHP Extension to handle common geospatial functions.

geospatial - PHP Geospatial Extension PHP Extension to handle common geospatial functions. The extension currently has implementations of the Haversin

PHP Geospatial, putting the Elephpant on your globe 56 Dec 29, 2022
GeoJSON implementation for PHP

GeoJson PHP Library This library implements the GeoJSON format specification. The GeoJson namespace includes classes for each data structure defined i

Jeremy Mikola 274 Dec 17, 2022
Gustavo Niemeyer's geocoding system

Geohash Library to convert a geohash to/from a latitude/longitude point, and to determine bounds of a geohash cell and find neighbours of a geohash. T

BESTE 3 Aug 31, 2022
Simple address and contact management for Laravel with automatically geocoding to add longitude and latitude

Laravel Addresses Simple address and contact management for Laravel with automatically geocoding to add longitude and latitude. Installation Require t

Chantouch Sek 2 Apr 4, 2022
A simple wrapper for (reverse) geocoding

Geocode - A simple wrapper for (reverse) geocoding Installation composer require markuskooche/geocode Usage How to geocode use Illuminate\Support\Col

Markus Koch 2 Jul 9, 2022
Laravel 8 Project Restrict User Access From IP Addresses. prevent other ip address that want to access over secure api or urls.

block-ip-address-laravel Laravel 8 Project Restrict User Access From IP Addresses. prevent other ip address that want to access over secure api or url

Hasmukh Dharajiya 2 Mar 24, 2022
Middleware to generate access logs for each request using the Apache's access log format

Middleware to generate access logs for each request using the Apache's access log format. This middleware requires a Psr log implementation, for example monolog.

Middlewares 20 Jun 23, 2022
Laravel Package for TMDB ( The Movie Database ) API. Provides easy access to the wtfzdotnet/php-tmdb-api library.

Laravel Package for TMDB API Wrapper A Laravel package that provides easy access to the php-tmdb/api TMDB (The Movie Database) API wrapper. This packa

PHP - The Movie Database 151 Nov 1, 2022
The library provides convenient access to the Epoint.az API from applications written in the PHP language.

Tural/Epoint library/SDK The library provides convenient access to the Epoint.az API from applications written in the PHP language. It includes a pre-

Tural 5 Jan 20, 2022
The library provides convenient access to the Epoint.az API from applications written in the PHP language.

Tural/Epoint library/SDK The library provides convenient access to the Epoint.az API from applications written in the PHP language. It includes a pre-

Tural 5 Jan 20, 2022