简体   繁体   中英

Receiving an output from shell_exec with non-standard characters

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!

I have actually found my answer with a long time with experimenting in case anyone ever comes across the same problem. I actually had to use header('Content-Type: text/html; charset=iso-8859-1'); at the very beginning of the file rather than using utf-8, and then it was able to correctly interface with the shell exec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM