php - 使用 file_get_contents()/PHP 在地理定位中进行字符编码

标签 php mysql character-encoding geolocation file-get-contents

我这样调用 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/

相关文章:

php - MySQL 表连接

html - 用日语渲染的 CSS?

linux - 为什么在 golang、linux 中使用 archive/zip 时文件名会变得困惑?

php - 在不重定向到另一个页面的情况下回显错误并保留所有表单数据?

php - xampp MySQL 的 INSERT 查询不起作用

php - Laravel 中需要什么 env 和 env.example 文件?

mysql - qt5.1.1 mysql ubuntu QMYSQL驱动未加载

javascript - 使用 javascript 和 while 循环隐藏/显示 div

mysql - sql插入时如何抑制唯一键检查

javascript - 编码错误信息