我这样调用 Google 地理定位服务:
$querystring = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&latlng=".$row['lat'].",".$row['lng'];
$context = stream_context_create(array(
'http' => array(
'timeout' => 10
)
));
$f = file_get_contents($querystring, 0, $context);
$f = json_decode($f);
if(!isset($f->status) || $f->status != 'OK') continue;
$f = $f->results[0];
$location = $f->formatted_address;
然后我将该位置插入数据库:
//add the location to the database
$sql = "INSERT INTO `wp_postmeta`
(`meta_id`, `post_id`, `meta_key`, `meta_value`)
VALUES
(
NULL,
'".intval($row['ID'])."',
'location',
'".mysql_real_escape_string($location)."'
)";
if($location) $insert = mysql_query($sql);
问题:当我编辑 wordpress 帖子时,有些字符显示不正确。 比如说 “R. Hilá¡rio Riberio, 41-243 - Praça da Bandeira, Rio de Janeiro, 20270-180, Brazil” 应该 “R. Hilário Riberio,41-243 - Praça da Bandeira,里约热内卢,20270-180,巴西”
我在网站上使用 UTF-8 作为字符编码。但这无关紧要,因为 phpMyAdmin 显示数据输入到数据库时也带有损坏的字符。
如何设置正确的字符编码?
最佳答案
使用mysql_set_charset
将数据库连接编码设置为'utf8'
,否则MySQL不知道你插入的是UTF-8编码的数据。有关详细信息,请参阅 Handling Unicode in a Web App .
关于php - 使用 file_get_contents()/PHP 在地理定位中进行字符编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071155/