PHPackages                             hi-folks/milk-sdk-php - 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. hi-folks/milk-sdk-php

ActiveLibrary[API Development](/categories/api)

hi-folks/milk-sdk-php
=====================

Milk SDK PHP is a (fluent) open-source PHP library that makes it easy to integrate your PHP application with location services

0.1.6(4y ago)81611[1 issues](https://github.com/Hi-Folks/milk-sdk-php/issues)MITPHPPHP ^7.4|^8.0

Since Oct 21Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Hi-Folks/milk-sdk-php)[ Packagist](https://packagist.org/packages/hi-folks/milk-sdk-php)[ Docs](https://github.com/hi-folks/milk-sdk-php)[ RSS](/packages/hi-folks-milk-sdk-php/feed)WikiDiscussions master Synced today

READMEChangelog (7)Dependencies (7)Versions (8)Used By (0)

Milk SDK PHP
============

[](#milk-sdk-php)

[![Actions Status](https://github.com/hi-folks/milk-sdk-php/workflows/PHP%20check%20test/badge.svg)](https://github.com/hi-folks/milk-sdk-php/actions)[![GitHub license](https://camo.githubusercontent.com/483eb61033b8efa6bdc5c380f795d28e56a844d144eda4cecf1448a6ab35d875/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f68692d666f6c6b732f6d696c6b2d73646b2d706870)](https://github.com/hi-folks/milk-sdk-php/blob/master/LICENSE.md)

[![Milk SDK PHP](milk-sdk-php.png "Milk SDK PHP")](milk-sdk-php.png)

Milk SDK PHP is a (fluent) open-source PHP library that makes it easy to integrate your PHP application with location services like:

- [HERE **Routing** API **V8**](#routing-api-v8) and [**V7**](#routing-api-v7): for calculate routes from a starting point to destination;
- [HERE **Weather** Destination API](#weather-api): for getting weather forecasts;
- [HERE **Geocoding** API](#geocoding-api): for converting addresses into geographic coordinates;
- [HERE **Reverse Geocoding** API](#reverse-geocoding-api): for converting coordinates into addresses;
- [HERE **Isoline** API](#isoline-api): for calculating the area that a driver (or a biker or a walker) can reach within a given time or distance;
- [HERE **Discover** API](#discover-api): for searching addresses, places and locations;
- [HERE **Map Image** API](#map-image-api): for creating static image of a map;
- [HERE **Data Hub** API](#use-here-data-hub);
- ... other coming soon ...

Getting Started
---------------

[](#getting-started)

### Install the SDK

[](#install-the-sdk)

In your PHP project install package via Composer:

```
composer require hi-folks/milk-sdk-php
```

Obtain HERE API Key
-------------------

[](#obtain-here-api-key)

To use HERE Location Services you will need an API key. The API key is a unique identifier that is used to authenticate API requests associated with your project. There is a official tutorial for retrieving the API Key:

Weather API
-----------

[](#weather-api)

With Weather API class you can get weather forecasts, observation, or alerts for a specific location.

You can call the following methods to get the corresponding weather information:

- *productObservation()*: Get current weather conditions from the eight closest locations to the specified location.
- *productAlerts()*: Get forecasted weather alerts for the next 24 hours.
- *productForecast7days()*: Get morning, afternoon, evening and night weather forecasts for the next seven days.
- *productForecast7daysSimple()*: Get daily weather forecasts for the next seven days.
- *productForecastAstronomy()*: Get information on when the sun and moon rise and set, and on the phase of the moon for the next seven days.
- *productForecastHourly()*: Get hourly weather forecasts for the next seven days.
- *productNwsAlerts()*: Get all active watches and warnings for the US and Canada.

For example, to retrieve weather forecasts in Berlin:

```
$jsonWeather = Weather::instance()
    ->setAppIdAppCode($hereAppId, $hereAppCode)
    ->productForecast7days()
    ->name("Berlin")
    ->get();
var_dump($jsonWeather->getData());
var_dump($jsonWeather->isError());
var_dump($jsonWeather->getErrorMessage());
var_dump($jsonWeather->getDataAsJsonString());
```

Routing API (v7)
----------------

[](#routing-api-v7)

To retrieve the fastest route by foot

```
$r = (new RoutingV7())
    ->setApiKey($hereApiKey)
    ->byFoot()
    ->typeFastest()
    ->startingPoint(52.5160, 13.3779)
    ->destination(52.5185, 13.4283)
    ->get();
var_dump($r->getData());
var_dump($r->isError());
var_dump($r->getErrorMessage());
var_dump($r->getDataAsJsonString());
```

Instead of using get(), you could use *getManeuverInstructions()* method:

```
$r = (new RoutingV7())
    ->setApiKey($hereApiKey)
    ->byFoot()
    ->typeFastest()
    ->startingPoint(52.5160, 13.3779)
    ->destination(52.5185, 13.4283)
    ->getManeuverInstructions();

var_dump($r);
```

Routing API (v8)
----------------

[](#routing-api-v8)

To retrieve the fastest route by car

```
$routingActions = RoutingV8::instance()
    ->setApiKey($hereApiKey)
    ->byCar()
    ->routingModeFast()
    ->startingPoint(52.5160, 13.3779)
    ->destination(52.5185, 13.4283)
    ->returnInstructions()
    ->langIta()
    ->getDefaultActions();

foreach ($routingActions as $key => $action) {
    echo " - ".$action->instruction . PHP_EOL;
}
```

Geocoding API
-------------

[](#geocoding-api)

In order to retrieve geo-coordinates (latitude, longitude) of a known address or place.

```
use HiFolks\Milk\Here\RestApi\Geocode;
$hereApiKey = "Your API KEY";
$r = Geocode::instance()
    ->setApiKey($hereApiKey)
    ->country("Italia")
    ->q("Colosseo")
    ->langIta()
    ->get();
var_dump($r->getData());
var_dump($r->isError());
var_dump($r->getErrorMessage());
var_dump($r->getDataAsJsonString());
```

Reverse Geocoding API
---------------------

[](#reverse-geocoding-api)

In order to find the nearest address to specific geo-coordinates:

```
use HiFolks\Milk\Here\RestApi\ReverseGeocode;
$hereApiKey = "Your API KEY";
$r = ReverseGeocode::instance()
    ->setApiKey($hereApiKey)
    ->at(41.88946,12.49239)
    ->limit(5)
    ->lang("en_US")
    ->get();
var_dump($r->getData());
var_dump($r->isError());
var_dump($r->getErrorMessage());
var_dump($r->getDataAsJsonString());

if ($r->isError()) {
    echo "Error: ". $r->getErrorMessage();
} else {
    $items = $r->getData()->items;
    foreach ($items as $key => $item) {
        echo " - " .$item->title.
            " : ( ".$item->position->lat . "," . $item->position->lng .
            " ) , distance:" . $item->distance . " , type: " . $item->resultType . PHP_EOL;
    }
}
```

Isoline API
-----------

[](#isoline-api)

```
use HiFolks\Milk\Here\RestApi\Isoline;
$hereApiKey = "yourapikey";
$isoline = Isoline::instance()
    ->setApiKey($hereApiKey)
    ->originPoint(41.890251, 12.492373)
    ->byFoot()
    ->rangeByTime(600) // 10 minutes
    ->get();
```

Map Image Api
-------------

[](#map-image-api)

With MapImage class you can create static image of a map. For the map you can define:

- *center()*: the center of the map;
- *addPoi()*: add a point in the map;
- *zoom()*: set the zoom level;
- *height()*: set the height of image (in pixel);
- *width()*: set the width of the image (in pixel).

```
use Hifolks\milk\here\RestApi\MapImage;
$hereApiKey = "yourapikey";
$imageUrl = MapImage::instance($hereApiKey)
    ->center(45.548, 11.54947)
    ->addPoi(45, 12, "ff0000")
    ->addPoi(45.1, 12.1, "00ff00")
    ->addPoi(45.2, 12.2, "0000ff", "", "12", "Test 3")
    ->zoom(12)
    ->height(2048)
    ->width(2048 / 1.4)
    ->getUrl();
```

You can use also the Geocoding functionality with *centerAddress()* method.

```
$image = MapImage::instance($hereApiKey)
    ->centerAddress("Venezia")
    ->zoom(12)
    ->height(2048)
    ->width(intval(2048 / 1.4));
$imageUrl = $image->getUrl();
```

Discover API
------------

[](#discover-api)

If you need to search an address or a place and you need to validate it and obtain some other information about the location, you can use *Discover API* endpoint via Discover class.

```
$address = "Basilica San Marco,  venezia";
// instance the class
$discover = Discover::instance($hereApiKey)
// define the address
    ->q($address)
// define a center point (for example Rome)
    ->at(41.902782, 12.496366)
// define where to limit the search, in this case ITA is the country code for Italy
    ->inCountry("ITA")
    ->get();
```

If you need to search an address in Italy, you can use inItaly() method:

```
$address = "Basilica San Marco,  venezia";
$discover = Discover::instance($hereApiKey)
    ->q($address)
    ->inItaly()
    ->get();
```

Use HERE Data Hub
-----------------

[](#use-here-data-hub)

### Configuring XYZ HUB

[](#configuring-xyz-hub)

With this SDK you can consume DataHub (XYZ) API. You have 2 options:

- use your own instance of XYZ HUB or
- use Data Hub Cloud [https://developer.here.com/documentation/studio/map\_customization\_suite\_hlp/dev\_guide/index.html](https://developer.here.com/documentation/studio/map_customization_suite_hlp/dev_guide/index.html)

#### Configure SDK with your own instance of XYZ HUB

[](#configure-sdk-with-your-own-instance-of-xyz-hub)

Running your own instance of XYZ HUB means that you already have your instance of . A tutorial about how to set up XYZ Hub locally (on localhost):

Create a *.env* file. Fill it with:

```
XYZ_ACCESS_TOKEN=""
XYZ_API_HOSTNAME="http://localhost:8080"

```

#### Configure SDK with XYZ HUB Cloud service

[](#configure-sdk-with-xyz-hub-cloud-service)

Using XYZ HUB Cloud Service means that you are using this host .

To use this service you need to sign in as developer on  and create your plan (for example Freemium) and obtain your Access Token.

Once you have your access token, create a *.env* file. You can start from a sample file (*.env.dist*):

```
cp .env.dist .env
```

Then, you need to fill your XYZ\_ACCESS\_TOKEN in .env file with your access token.

Quick Examples
--------------

[](#quick-examples)

In order to use the Milk SDK, you need to:

- create a PHP file
- include the autoload.php file
- declare all imports via *use*
- load environment configuration (via *Dotenv*)
- get your token
- get your XYZ Spaces
- display your result

```
