Geonames API, using while() for getting names of countries - php

I need to get all countries with geonames api. I can use $geonames->genomes[2]->countryName to get country name of 2nd. But I want it auto like while() how can I use it with while() in php?
JSON IS HERE
{"geonames":[{"countryName":"Andorra","currencyCode":"EUR","fipsCode":"AN","countryCode":"AD","isoNumeric":"020","north":42.65604389629997,"capital":"Andorra la Vella","continentName":"Europe","areaInSqKm":"468.0","languages":"ca","isoAlpha3":"AND","continent":"EU","south":42.42849259876837,"east":1.7865427778319827,"geonameId":3041565,"west":1.4071867141112762,"population":"84000"},{"countryName":"United Arab Emirates","currencyCode":"AED","fipsCode":"AE","countryCode":"AE","isoNumeric":"784","north":26.08415985107422,"capital":"Abu Dhabi","continentName":"Asia","areaInSqKm":"82880.0","languages":"ar-AE,fa,en,hi,ur","isoAlpha3":"ARE","continent":"AS","south":22.633329391479492,"east":56.38166046142578,"geonameId":290557,"west":51.58332824707031,"population":"4975593"}]}

We have a CSV file that has all the countries in the world, their corresponding ISO codes, and their short and formal names in English, French, Spanish, Russian, Chinese, Arabic, and Italian. This was compiled from various government and international references:
http://www.opengeocode.org/download/countrynames.txt
References are cited at:
http://www.opengeocode.org/download.php#countrynames

You can use a for loop:
for ($i = 0; $i < $numCountries; $i++) {
echo $geonames->geonames[$i]->countryName;
}

Related

City name, County name returning in other languages not in English

When i search using below FQ API :
https://api.foursquare.com/v2/venues/search?ll=19.346523,-99.191292&query=gimn&oauth_token=Z1NASAZ5IEUSDJAJG1VMQO5YX410DJXUKBKAHXN0FYIB15BQ&v=20150401
In browser we got the City name as "Mexico City".
But, when i used this API call in file_get_contents or curl, city name is returning as "Ciudad de México" in spanish language.
I need City name in English, Pls provide me the steps to fix this issue.
Thanks,
Suhanya.M
It's defaulting to Spanish, as it's the most popular for that locale. You can specify locale=en for English:
https://developer.foursquare.com/overview/versioning
Here's what the new link would look like; note the additional parameter at the very end:
https://api.foursquare.com/v2/venues/search?ll=19.346523,-99.191292&query=gimn&oauth_token=Z1NASAZ5IEUSDJAJG1VMQO5YX410DJXUKBKAHXN0FYIB15BQ&v=20150401&locale=en

Region name for an ISO 3166-1 alpha-2 country code

In a database we have ISO 3166-1 alpha-2 country codes, e.g. GB for United Kingdom and AR for Argentina. Today we keep a database table with one column for these country codes and one for corresponding English country names.
We work in PHP (5.4.x) which has the intl extenion (wrapper for ICU library) so I thought I'd like to fetch the country names from this source which makes the application more maintainable and easier to internationalize as I easily can fetch country names in German, French or whatever we want in the future.
Locale::getDisplayRegion ( string $locale [, string $in_locale ] ) provides a region name from a locale.
E.g.
php > echo Locale::getDisplayRegion('en-US', 'de') . PHP_EOL;
Vereinigte Staaten
php > echo Locale::getDisplayRegion('en-US', 'en') . PHP_EOL;
United States
The problem is that I do not have the language code, only the country code, so I'm not sure how to build up a proper locale.
It seems like I can get the region names by just prefixing with 'en_' for English, e.g
php > echo Locale::getDisplayRegion('en-CH', 'en') . PHP_EOL;
Switzerland
php > echo Locale::getDisplayRegion('en-CN', 'en') . PHP_EOL;
China
php > echo Locale::getDisplayRegion('en-AR', 'en') . PHP_EOL;
Argentina
However, this feels more like a hack and I'm not sure I really can rely on that?
I was thinking if I could somehow compose a locale using the country code (region / territory depending on terminology) and any of the valid languages for the country, but it appears it doesn't work as far as I've found at least:
php > echo Locale::composeLocale( array('region' => 'CN') ) . PHP_EOL;
php >
Any recommendations on how to go about this?
I do not want to maintain my own database of country names or simply use an external PHP library for it when there's a built-in extension which seem to be a good source (ICU) already.
Thanks a lot in advance!
use und (unknown language). So 'und_' . 'AR' etc.

php mysql insert not working when including specific text

I have a text file that looks like this:
<http://dbpedia.org/resource/Autism> <http://www.w3.org/2000/01/rdf-schema#comment> "Autism is a disorder of neural development characterized by impaired social interaction and communication, and by restricted and repetitive behavior. The diagnostic criteria require that symptoms become apparent before a child is three years old. Autism affects information processing in the brain by altering how nerve cells and their synapses connect and organize; how this occurs is not well understood."#en .
<http://dbpedia.org/resource/Anarchism> <http://www.w3.org/2000/01/rdf-schema#comment> "Anarchism is generally defined as the political philosophy which holds the state to be undesirable, unnecessary, and harmful, or alternatively as opposing authority and hierarchical organization in the conduct of human relations. Proponents of anarchism, known as \"anarchists\", advocate stateless societies based on non-hierarchical voluntary associations. There are many types and traditions of anarchism, not all of which are mutually exclusive."#en .
<http://dbpedia.org/resource/Achilles> <http://www.w3.org/2000/01/rdf-schema#comment> "In Greek mythology, Achilles was a Greek hero of the Trojan War, the central character and the greatest warrior of Homer's Iliad. Plato named Achilles the most handsome of the heroes assembled against Troy. Later legends (beginning with a poem by Statius in the 1st century AD) state that Achilles was invulnerable in all of his body except for his heel. As he died because of a small wound on his heel, the term Achilles' heel has come to mean a person's principal weakness."#en .
I'm using code (not relevant here) to extract the name of the article in the first url in each line. Then I extract the first sentence of the description between quotes. The problem is when I try to insert that first sentence string into my table, the insert fails (echoing works fine). Just inserting the title without the description works fine. Does anyone have any idea why the description makes the insert fail?
Here's the code I'm using to get the first sentence:
$data = fgets($handle); //get line
$data = str_replace("> ", "-!-", $data);
dataArr = explode("-!-", $data);
//Get last part of uri from 1st element in array
$title = getLastPartOfUrl($dataArr[0]);
$desc=preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', escape(substr($dataArr[2],1)));
$db->query("insert into mytable SET title = '".$title."', desc ='".$desc."'");
function escape($str)
{
$search=array("\\","\0","\n","\r","\x1a","'",'"');
$replace=array("\\\\","\\0","\\n","\\r","\Z","\'",'\"');
return str_replace($search,$replace,$str);
}
EDIT: I tried both urlencode and addslashes to no avail, in both cases including the $desc string makes the insert fail.
You don't escape the title.
I wouldn't trust your escape function either. I'm not sure what $db is, but you should use properly parameterized queries with PDO/mysqli
EDIT: DESC is a reserved word in MySQL. You need to surround it (when used as a column name) with backticks in your query.

Geohash in redis with php

I am using Redis with php and its library phpredis.
I have the following structure:
city:1 {
lat->14.02,
lon->10.00,
hash->sp2f1h60w5j
}
city:2 {
lat->14.03,
lon->10.1,
hash->sp2f1h60w5m
}
But I haven't found a way to search by hash. I would like to search for example the exact same hash or approximately the same hash.
Should I change my structure?
Thanks.
You could follow Josiah Carlson's advice from this thread, and convert the hash into a number, and use that as a score in a sorted set. Something like this:
city:1 {
lat->14.02,
lon->10.00,
hash->sp2f1h60w5j
}
city:2 {
lat->14.03,
lon->10.1,
hash->sp2f1h60w5m
}
To use the geohashes as numbers, you need to decode them from base 32 using a particular character map -- see Geohash on Wikipedia. I'll use example numbers below.
cities { (1, 4711), (2, 4712) }
Now you can use zrangebyscore to find cities in an area:
zrangebyscore cities 4000 5000
Why you don't use the key in redis to get the elements.
like:
city-sp2f1h60w5j = 1
city-sp2f1h60w5m = 2
then if you wish the closets:
get by "key":
city-sp2f1h60w*
if will return "array" of keys... or city id's. Can't remember the exact return data.
cheers
Do you want to find all items near a specific point? Since you're looking for exact or near match, I assume that's the case. Redis just released brand new Geo functionality yesterday (http://redis.io/commands#geo).
GeoRadius and GeoRadiusByMember are the ones you're looking for.

multi-array search query with delimited field

I have a array which store some country names
im doing a search module
and i wanna to find the db records whether contain the country names or not
but since db design problem , those record can own mutil counties and store the countries by delimiter "|"
below is a example
input: array("Cyprus","Austria") // note that the max input will be 300 country
db record country column: Albania|Andorra|Austria|Belarus|Belgium|Bosnia and Herzegovina|Bulgaria|Croatia|Cyprus|Czech Republic|Denmark|Estonia|European Union|Faroe Islands
So anyone can give a solution base on php for seaching?
First of all your DB design is not good. You should not be putting multiple delimited country names in one filed. You might wanna change it.
If you really want to work with the existing design, you can make use of explode and in_array functions of PHP as:
$db_rec = 'Albania|Andorra|Austria|Belarus|Belgium|Bosnia and Herzegovina';
$arr = explode('|',$db_rec);
$key = 'Albania';
if(in_array($key,$arr)) {
echo "$key found";
}

Categories