Skip to content

PHP SDK

Add geocoding, search, routing, and more to your website or backend with just a few lines of code.

Our PHP SDK includes built-in type information and comprehensive docstrings straight from our official specs. The SDK includes type hints and docstrings straight from our spec, so editors like PhpStorm can guide your coding with documentation tooltips and autocomplete. With few dependencies and a close adherence to the API spec, there's no easier way to leverage our APIs in PHP.

Quickstart

Installation

Composer

To install the package via Composer, add stadiamaps/stadiamaps-api-php to your composer.json:

{
  "require": {
    "stadiamaps/stadiamaps-api-php": "1.*"
  }
}

Then run composer install.

Manual Installation

You can also download the files manually and include autoload.php in your scripts:

<?php
require_once('/path/to/OpenAPIClient-php/vendor/autoload.php');

Getting an API Key

You'll need an API key to use the client in your project.

  1. Sign in to the client dashboard. (If you don't have an account yet, sign up for free; no credit card required!)
  2. Click "Manage Properties."
  3. If you have more than one property (ex: for several websites or apps), make sure you have selected the correct property from the dropdown at the top of the page.
  4. Under "Authentication Configuration," you can generate, view or revoke your API key.

Video: How to generate your API key

Example Code

Here's a quick usage example of how to set up a client and do some geocoding.

<?php
// use or require, depending on your installation method.

// Configure API key authorization (replace with your Stadia Maps API key) (1)
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('api_key', 'YOUR-API-KEY');
// You can also use our EU endpoint to keep traffic within the EU using setHost:
// $config = Configuration::getDefaultConfiguration()->setApiKey('api_key', 'YOUR-API-KEY')->setHost('https://api-eu.stadiamaps.com');

$apiInstance = new OpenAPI\Client\Api\GeocodingApi(
    new GuzzleHttp\Client(),
    $config
);

try {
    $text = '1600 Pennsylvania Ave NW'; // The place name (address, venue name, etc.) to search for.
    $result = $apiInstance->autocomplete($text);

    // Do something with the response...
    print_r($result);
} catch (Exception $e) {
    // Add your error handling here
    echo 'Exception when calling the Stadia Maps API: ', $e->getMessage(), PHP_EOL;
}
  1. Learn how to get an API key in our authentication guide.

You can find additional usage examples on the endpoint documentation pages.

Source Code

Our PHP SDK is completely open source! You can find the source code on GitHub.