Milk SDK PHP is a (fluent) open-source PHP library that makes it easy to integrate your PHP application with location services like:
- HERE Data Hub API (was XYZ Api);
- HERE Routing API (V8 and V7);
- HERE Weather Destination API;
- HERE Geocoding API;
- HERE Reverse Geocoding API;
- HERE Isoline API;
In your PHP project install package via Composer:
composer require hi-folks/milk-sdk-php
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
Running your own instance of XYZ HUB means that you already have your instance of https://github.com/heremaps/xyz-hub. A tutorial about how to set up XYZ Hub locally (on localhost): https://dev.to/robertobutti/restful-web-service-for-geospatial-data-12ii
Create a .env file. Fill it with:
XYZ_ACCESS_TOKEN=""
XYZ_API_HOSTNAME="http://localhost:8080"
Using XYZ HUB Cloud Service means that you are using this host https://xyz.api.here.com.
To use this service you need to sign in as developer on https://developer.here.com/ 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.
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
<?php
// include the autoload.php file
require "./vendor/autoload.php";
// declare all imports via "use"
use HiFolks\Milk\Here\Xyz\Space\XyzSpace;
// set your token
$xyzToken = "your xyz space token";
// Get your XYZ Spaces (XyzResponse class)
$s = XyzSpace::instance($xyzToken)->get();
// display your result
var_dump($s->getData());
To get your XYZ Spaces:
$s = XyzSpace::instance($xyzToken)->get();
To get XYZ Spaces by everybody (not only your own XYZ Spaces):
$s = XyzSpace::instance($xyzToken)->ownerAll()->get();
To delete a XYZ Space:
$xyzSpaceDeleted = XyzSpace::instance($xyzToken)->delete($spaceId);
To create a new XYZ Space:
$xyzSpaceCreated = XyzSpace::instance($xyzToken)->create("My Space", "Description");
To update the XYZ Space with space id == $spaceId:
$obj = new \stdClass;
$obj->title = "Edited Title";
$obj->description = "Edited Description";
$retVal = $space->update($spaceId, $obj);
The get statistics from XYZ Space:
$statistics = XyzSpaceStatistics::instance($xyzToken)->spaceId($spaceId)->get();
Iterate features
/** XyzSpaceFeature $xyzSpaceFeature */
$xyzSpaceFeature = new XyzSpaceFeature::instance($xyzToken);
$result = $xyzSpaceFeature->iterate($spaceId)->get();
You need to use feature() method with $featureId and $spaceId
$xyzSpaceFeature = XyzSpaceFeature::instance($xyzToken);
$result = $xyzSpaceFeature->feature($featureId, $spaceId)->get();
To create or edit a Feature you can use saveOne() method.
$spaceId = "yourspaceid";
$featureId = "yourfeatureid";
$geoJson = new GeoJson();
$properties = [
"name" => "Berlin",
"op" => "Put"
];
$geoJson->addPoint(52.5165, 13.37809, $properties, $featureId);
$feature = XyzSpaceFeatureEditor::instance($xyzToken);
$result = $feature->feature($geoJson->get())->saveOne($spaceId, $featureId);
$feature->debug();
If you have a Geojson File, you can upload it into a space.
$spaceId = "yourspaceid";
$file = "https://data.cityofnewyork.us/api/geospatial/arq3-7z49?method=export&format=GeoJSON";
$response = XyzSpaceFeatureEditor::instance($xyzToken)
->addTags(["file"])
->geojson($file)
->create($spaceId);
To search features by properties you can use addSearchParams to add serach params, in the example below, you are searching features with name property equals "Colosseo".
$spaceId = "yourspaceid";
$xyzSpaceFeature = XyzSpaceFeature::instance($xyzToken)->addSearchParams("p.name", "Colosseo");
$result = $xyzSpaceFeature->search($spaceId)->get();
To search feature close to latitude=41.890251 and longitude=12.492373 with a radius less than 1000 meters (close to Colosseum):
$spaceId = "yourspaceid";
$result = XyzSpaceFeature::instance($xyzToken)->spatial($spaceId, 41.890251, 12.492373, 1000)->get();
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());
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);
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;
}
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());
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;
}
}
use HiFolks\Milk\Here\RestApi\Isoline;
$hereApiKey = "yourapikey";
$isoline = Isoline::instance()
->setApiKey($hereApiKey)
->originPoint(41.890251, 12.492373)
->byFoot()
->rangeByTime(600) // 10 minutes
->get();
use Hifolks\milk\here\RestApi\MapImage;
$hereApiKey = "yourapikey";
$image = 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();
print_line("Image", $image);
- ReDoc API documentation: https://xyz.api.here.com/hub/static/redoc/
- Open API documentation: https://xyz.api.here.com/hub/static/swagger/
- Overview: https://developer.here.com/documentation/destination-weather/dev_guide/topics/overview.html
- API Reference: https://developer.here.com/documentation/destination-weather/dev_guide/topics/api-reference.html