graphhopper / directions-api

Issues for the GraphHopper Directions API
https://graphhopper.com/api/1/docs/
60 stars 25 forks source link

PHP client #11

Open karussell opened 9 years ago

karussell commented 9 years ago

We got a request asking for how to do an API request via PHP. The most important part is to use the correct URL:

<?
$adr='https://graphhopper.com/api/1/geocode?q=' . urlencode('Deutschland Berlin') . '&locale=de&key=[YOUR_KEY]';
$str=file_get_contents($adr);
echo $str;
?>

Or for the routing API:

<?
$adr='https://graphhopper.com/api/1/route?point=' . lat1 . ',' . lon1 . '&point=' . lat2 . ',' . lon2 . '&locale=de&vehicle=car&key=[YOUR_KEY]';
$str=file_get_contents($adr);
echo $str;
?>

Then use the $str for further JSON parsing or use httpful a HTTP client making such requests easy for PHP.

Our route optimization API contains a swagger specification from which you can easily create an already working PHP client. Contact us if you do not know how to handle that.

webdevilopers commented 7 years ago

For anyone using @Guzzle and @Symfony Components:

use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncoder;

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', $uri);

$decoder = new JsonDecode();
$routing = $decoder->decode($response->getBody()->getContents(), JsonEncoder::FORMAT);