I get the following error:
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: GET ;street=Kantatenweg+25&city=Leipzig&postalcode=04229&country=Germany&addressdetails=1
resulted in a 403 Forbidden
response:
<html> <head> <title>Access blocked</title> </head> <body> <h1>Access blocked</h1> <p>You have been blocked because you have violated the usage policy of OSM's Nominatim geocoding service. Please be aware that OSM's resources are limited and shared between many users. The usage policy is there to ensure that the service remains usable for everybody.
...
This is what my source text looks like. I am grateful for any advice.
<?php
use maxh\Nominatim\Nominatim;
use GuzzleHttp\Client;
$adresse = [
'strasse' => $strasseXml,
'ort' => $datensatz['Ort'],
'plz' => $datensatz['PLZ']
];
$url = "/";
$defaultHeader = [
'verify' => false,
'headers', array('User-Agent' => 'api_client')
];
$client = new Client([
'base_uri' => $url,
'timeout' => 30,
'connection_timeout' => 5,
]);
$nominatim = new Nominatim($url);
$search = $nominatim->newSearch()
->street($adresse['strasse'])
->city($adresse['ort'])
->postalCode($adresse['plz'])
->country('Germany')
->addressDetails();
try {
$ergebnis = $nominatim->find($search);
} catch (\GuzzleHttp\Exception\ClientException $e) {
echo "Fehlermeldung: " . $e->getMessage() . "\n";
echo "HTTP-Code: " . $e->getResponse()->getStatusCode() . "\n";
echo "Antwort: " . $e->getResponse()->getBody()->getContents() . "\n";
}
I am submitting the address Kantatenweg+25, Leipzig, 04229, Germany + addressdetails=1 and would like to receive the coordinates.
After displaying the complete error code, I know what to do.
Many thanks to @c3roe!
I get the following error:
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: GET https://nominatim.openstreetmap.org/search?format=json&street=Kantatenweg+25&city=Leipzig&postalcode=04229&country=Germany&addressdetails=1
resulted in a 403 Forbidden
response:
<html> <head> <title>Access blocked</title> </head> <body> <h1>Access blocked</h1> <p>You have been blocked because you have violated the usage policy of OSM's Nominatim geocoding service. Please be aware that OSM's resources are limited and shared between many users. The usage policy is there to ensure that the service remains usable for everybody.
...
This is what my source text looks like. I am grateful for any advice.
<?php
use maxh\Nominatim\Nominatim;
use GuzzleHttp\Client;
$adresse = [
'strasse' => $strasseXml,
'ort' => $datensatz['Ort'],
'plz' => $datensatz['PLZ']
];
$url = "http://nominatim.openstreetmap.org/";
$defaultHeader = [
'verify' => false,
'headers', array('User-Agent' => 'api_client')
];
$client = new Client([
'base_uri' => $url,
'timeout' => 30,
'connection_timeout' => 5,
]);
$nominatim = new Nominatim($url);
$search = $nominatim->newSearch()
->street($adresse['strasse'])
->city($adresse['ort'])
->postalCode($adresse['plz'])
->country('Germany')
->addressDetails();
try {
$ergebnis = $nominatim->find($search);
} catch (\GuzzleHttp\Exception\ClientException $e) {
echo "Fehlermeldung: " . $e->getMessage() . "\n";
echo "HTTP-Code: " . $e->getResponse()->getStatusCode() . "\n";
echo "Antwort: " . $e->getResponse()->getBody()->getContents() . "\n";
}
I am submitting the address Kantatenweg+25, Leipzig, 04229, Germany + addressdetails=1 and would like to receive the coordinates.
After displaying the complete error code, I know what to do.
Many thanks to @c3roe!
You need to set a User Agent on the Client. I tested this snippet so you should be fine with just pasting it for testing purposes but i recommend to outsource the actual string to a config file.
$client = new Client([
'base_uri' => $url,
'timeout' => 30,
'connection_timeout' => 5,
'headers' => ['User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'],
]);
In my case, I'm using Guzzle through the Laravel 12 HTTP Client. To resolve the issue, I had to include a custom user-agent header along with either acceptJson()
or accept('application/json')
.
Using Guzzle directly:
$client->request('GET', $url, [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
'Accept' => 'application/json',
]
]);
https://docs.guzzlephp.org/en/stable/request-options.html#headers
Using Laravel HTTP Client (Laravel 8 and later):
\Illuminate\Support\Facades\Http::acceptJson()->withHeaders([
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'
])->get($url);
https://laravel.com/docs/12.x/http-client#headers