I am trying to get the result of a Python program executed through PHP, which passes in the parameter of coordinates, and prints the address at those coordinates.
Eg, when the coordinates 46.07018635,3.93749893 - randomly chosen on Google Maps, don't worry I'm not doxxing myself here - are used, I get the address Renaison, Roanne, Loire, Auvergne-Rhône-Alpes, Metropolitan France, 42370, France. This code works perfectly for coordinates whose address is in English, but when a character like ô appears, when $output is echoed, it shows as a �.
The code for executing the python is:
$code = 'python ./reverseGeoloc.py ' . $lat . ',' . $long;
$command = escapeshellcmd($code);
$output = shell_exec($command);
And the Python itself is:
import sys
coords = sys.argv[1]
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="my_request")
location = geolocator.reverse(coords, language='en')
print(location.address)
So, echoing $output with these coordinates gives:
Boucherie Charcuterie, Rue du Commerce, Les �tangs-Nord, Chazelles, Renaison, Roanne, Loire, Auvergne-Rh�ne-Alpes, Metropolitan France, 42370, France
.
This does print correctly if ran in isolation in the terminal, giving Renaison, Roanne, Loire, Auvergne-Rhône-Alpes, Metropolitan France, 42370, France
as expected.
I have tried literally everything I could find Googling, but so far nothing has worked!
Any help is appreciated!
Thank you!
My Answer:
'reverseGeoloc')_location = geolocator.reverse(coords)_print(location.address)__I suspect that the issue lies in the encoding of the characters in the output from the Python script. You can try setting the encoding explicitly in the Python script before printing the address:_print(location.address.encode('utf-8'))__This should ensure that the non-standard characters are properly encoded and displayed correctly when echoed in PHP.
Rate this post
3 of 5 based on 2348 votesComments