PHPackages                             opencage/geocode - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. opencage/geocode

ActiveLibrary[API Development](/categories/api)

opencage/geocode
================

A PHP library for geocoding via the OpenCage Geocoder API

v4.0.1(1w ago)37523.2k↓51.4%94BSD-2-ClausePHPPHP &gt;=8.2CI failing

Since Jan 17Pushed 1w ago9 watchersCompare

[ Source](https://github.com/opencagedata/php-opencage-geocode)[ Packagist](https://packagist.org/packages/opencage/geocode)[ RSS](/packages/opencage-geocode/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (10)Versions (33)Used By (4)

OpenCage Geocoding API Library for PHP
======================================

[](#opencage-geocoding-api-library-for-php)

A [PHP](http://php.net/) library to use the [OpenCage geocoding API](https://opencagedata.com/api).

Build Status / Code Quality
---------------------------

[](#build-status--code-quality)

[![Build Status](https://github.com/OpenCageData/php-opencage-geocode/actions/workflows/ci.yml/badge.svg)](https://github.com/OpenCageData/php-opencage-geocode/actions/workflows/ci.yml)[![PHP version](https://camo.githubusercontent.com/e90c76667589137e819231ce7f87ee608cdf878657ee47cb193958b794761340/68747470733a2f2f62616467652e667572792e696f2f70682f6f70656e6361676525324667656f636f64652e737667)](https://badge.fury.io/ph/opencage%2Fgeocode)[![Mastodon Follow](https://camo.githubusercontent.com/6b6b582f61b0c04de6e9dab353430437189bd3ae0c9378ad0ed843aa7dc0029d/68747470733a2f2f696d672e736869656c64732e696f2f6d6173746f646f6e2f666f6c6c6f772f3130393238373636333436383530313736393f646f6d61696e3d6874747073253341253246253246656e2e6f736d2e746f776e253246267374796c653d736f6369616c)](https://camo.githubusercontent.com/6b6b582f61b0c04de6e9dab353430437189bd3ae0c9378ad0ed843aa7dc0029d/68747470733a2f2f696d672e736869656c64732e696f2f6d6173746f646f6e2f666f6c6c6f772f3130393238373636333436383530313736393f646f6d61696e3d6874747073253341253246253246656e2e6f736d2e746f776e253246267374796c653d736f6369616c)

Overview
--------

[](#overview)

This library uses [Guzzle](https://docs.guzzlephp.org/en/stable/) to access the OpenCage Geocoding API. Both synchronous and asynchronous requests are supported.

### Authentication

[](#authentication)

You need an API key, which you can sign up for [here](https://opencagedata.com).

### Tutorial

[](#tutorial)

You can find [a comprehensive tutorial for using this module on the OpenCage site](https://opencagedata.com/tutorials/geocode-in-php).

Working with AI / Agent Skill
-----------------------------

[](#working-with-ai--agent-skill)

There is an [Agent Skill for working with the OpenCage Geocoding API](https://github.com/OpenCageData/opencage-skills/) which includes a reference file for developing in PHP using this library.

Installation
------------

[](#installation)

Requires PHP 8.2 or newer.

### With Composer

[](#with-composer)

The recommended - and easiest way - to install is via [Composer](https://getcomposer.org/). Require the library in your project's `composer.json` file.

```
$ composer require opencage/geocode
```

Import the Geocoder class.

```
require "vendor/autoload.php";
```

### The old fashioned way

[](#the-old-fashioned-way)

See the file `demo/geocode.php`

Geocoding
---------

[](#geocoding)

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

### Reverse geocoding

[](#reverse-geocoding)

```
$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
$result = $geocoder->geocodeReverse(43.831, 4.360); # latitude, longitude
print $result['results'][0]['formatted'];
// 3 Rue de Rivarol, 30020 Nîmes, France
```

### Set optional parameters

[](#set-optional-parameters)

See the full list at:

```
$result = $geocoder->geocode('6 Rue Massillon, 30020 Nîmes, France', [
    'language' => 'fr',
    'countrycode' => 'fr'
]);
if ($result && $result['total_results'] > 0) {
  $first = $result['results'][0];
  print $first['geometry']['lng'] . ';' . $first['geometry']['lat'] . ';' . $first['formatted'] . "\n";
  // 4.360081;43.8316276;6 Rue Massillon, 30020 Nîmes, Frankreich
}
```

### Async geocoding

[](#async-geocoding)

```
$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');

// single async request
$promise = $geocoder->geocodeAsync('82 Clerkenwell Road, London');
$result = $promise->wait();
print_r($result);

// concurrent requests
$promises = [
    'london' => $geocoder->geocodeAsync('London'),
    'paris'  => $geocoder->geocodeAsync('Paris'),
    'tokyo'  => $geocoder->geocodeAsync('Tokyo'),
];
$results = \GuzzleHttp\Promise\Utils::unwrap($promises);
print $results['london']['results'][0]['formatted'];
print $results['paris']['results'][0]['formatted'];
print $results['tokyo']['results'][0]['formatted'];

// async reverse geocoding
$promise = $geocoder->geocodeReverseAsync(43.831, 4.360);
$result = $promise->wait();
print $result['results'][0]['formatted'];
```

Configuration
-------------

[](#configuration)

### Set a timeout

[](#set-a-timeout)

```
$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
$geocoder->setTimeout(5); // seconds, default is 10
$result = $geocoder->geocode('82 Clerkenwell Road, London');
```

### Set a proxy URL

[](#set-a-proxy-url)

The proxy URL must include a scheme (`http`, `https`, or `socks5`) and a host.

```
$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
$geocoder->setProxy('https://proxy.example.com:1234');
$result = $geocoder->geocode("Brandenburger Tor, Berlin");
print_r($result['results'][0]['formatted']);
// Brandenburger Tor, Unter den Linden, 10117 Berlin, Germany
print_r($result['results'][0]['geometry']);
// Array
// (
//    [lat] => 52.5166047
//    [lng] => 13.3809897
// )
```

Example results
---------------

[](#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)

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

Who is OpenCage GmbH?
---------------------

[](#who-is-opencage-gmbh)

[![](opencage_logo_300_150.png)](https://opencagedata.com)

We run a worldwide [geocoding API](https://opencagedata.com/api) and [geosearch](https://opencagedata.com/geosearch) service based on open data. Learn more [about us](https://opencagedata.com/about).

We also run [Geomob](https://thegeomob.com), 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](https://thegeomob.com/podcast/).

\-- end --

###  Health Score

69

—

FairBetter than 100% of packages

Maintenance98

Actively maintained with recent releases

Popularity49

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity87

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 59.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~192 days

Recently: every ~49 days

Total

17

Last Release

10d ago

Major Versions

2.1.0 → v3.0.12021-01-28

v3.4.0 → v4.0.02026-06-08

PHP version history (5 changes)2.0.1PHP &gt;=5.6.0

v3.0.1PHP &gt;=7.0

v3.2.0PHP &gt;=8.0

v3.3.2PHP &gt;=8.1

v3.4.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/647fee0ede452e7c36460407838a59f1d29e7a4ac281635e8e08cfb8e7c563e0?d=identicon)[devopencage](/maintainers/devopencage)

---

Top Contributors

[![mtmail](https://avatars.githubusercontent.com/u/3727288?v=4)](https://github.com/mtmail "mtmail (59 commits)")[![freyfogle](https://avatars.githubusercontent.com/u/351074?v=4)](https://github.com/freyfogle "freyfogle (21 commits)")[![tsamaya](https://avatars.githubusercontent.com/u/1741320?v=4)](https://github.com/tsamaya "tsamaya (14 commits)")[![vicchi](https://avatars.githubusercontent.com/u/442617?v=4)](https://github.com/vicchi "vicchi (3 commits)")[![edgarsstrods](https://avatars.githubusercontent.com/u/25812122?v=4)](https://github.com/edgarsstrods "edgarsstrods (1 commits)")[![sbscully](https://avatars.githubusercontent.com/u/418553?v=4)](https://github.com/sbscully "sbscully (1 commits)")

---

Tags

geocodingopencageopencage-geocoderphpgeocodingaddressesgeocodeopencagedata

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/opencage-geocode/health.svg)

```
[![Health](https://phpackages.com/badges/opencage-geocode/health.svg)](https://phpackages.com/packages/opencage-geocode)
```

###  Alternatives

[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3741.3M47](/packages/tencentcloud-tencentcloud-sdk-php)[eslazarev/wildberries-sdk

Wildberries OpenAPI clients (generated).

273.0k](/packages/eslazarev-wildberries-sdk)[files.com/files-php-sdk

Files.com PHP SDK

2481.1k](/packages/filescom-files-php-sdk)[aimeos/prisma

A powerful PHP package for integrating media related Large Language Models (LLMs) into your applications

1943.1k5](/packages/aimeos-prisma)[jcf/geocode

Google Geocoding API for Laravel

50162.8k](/packages/jcf-geocode)[volcengine/volcengine-php-sdk

118.7k](/packages/volcengine-volcengine-php-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
