I call the Google geolocation service like this:
$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;
I then insert the location into the database:
//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);
The problem: Some characters are not well displayed when I edit the wordpress posts.
Like for example
“R. Hilário Riberio, 41-243 – Praça da Bandeira, Rio de Janeiro, 20270-180, Brazil”
should be
“R. Hilário Riberio, 41-243 – Praça da Bandeira, Rio de Janeiro, 20270-180, Brazil”
I use UTF-8 as character encoding on the website. But that’s irrelevant, because phpMyAdmin shows the data is entered into the database with the broken characters as well.
How do I set the right character encoding?
Use
mysql_set_charsetto set the database connection encoding to'utf8', otherwise MySQL doesn’t know that you’re inserting UTF-8 encoded data. For more information, see Handling Unicode in a Web App.