Giter Club home page Giter Club logo

geonames-client's Introduction

GeoNames Client

A GeoNames API Client for PHP.

validate

Install with confidence ๐Ÿ›ก๏ธ

  • Supported OS: Linux, macOS and Windows.
  • Supported PHP versions: 7.2 and up.

Quick Start

An overview of available API parameters for each endpoint is available here.

<?php

use GeoNames\Client as GeoNamesClient;

$g = new GeoNamesClient('username');

// get a list of supported endpoints
$endpoints = $g->getSupportedEndpoints();

// get info for country
// note that I'm using the array destructor introduced in PHP 7.1
[$country] = $g->countryInfo([
    'country' => 'IL',
    'lang'    => 'ru', // display info in Russian
]);

// country name (in Russian)
$country_name = $country->countryName;

// spoken languages (ISO-639-1)
$country_languages = $country->languages;

Why?

This library will allow you to get better insights into the world.

As a developer and a multi-lingual speaker I've always felt that localization was put on last priority since it was so time consuming and error-prone.

Getting statistics for each country is a painful process that requires understanding the different ISO standards, and even then you're still left to piece the puzzle together yourself.

Luckily, GeoNames have been collecting statistical data about the world for the past few decades and offers that data via their API.

The aim of this library is to provide a single source of truth for country (ISO-3166), language (ISO-639-1), and other locale related statistical data, so that other developers can write better software which is up-to-date with the latest changes in the world.

Installation

If you're using Composer to manage dependencies:

composer require aternus/geonames-client

Then, after running composer update, you can load the class using Composer's autoloading:

require 'vendor/autoload.php';

Otherwise, you can simply require the file directly:

require_once 'vendor/aternus/geonames-client/src/Client.php';

And in either case, I'd suggest using an alias.

use GeoNames\Client as GeoNamesClient;

Other Useful Libraries

Please make sure to implement some kind of a cache mechanism in order to save yourself time, bandwidth and be respectful to GeoNames for providing all that data for free.

If you're making heavy use of the statistical data, you can subscribe to the their Premium Data plan.

License

Released under the MIT License - see LICENSE.md for details.

Credits

David Jean Louis for the PEAR package which inspired this GeoNames API Client.

geonames-client's People

Contributors

alesak avatar aternus avatar bhujagendra-ishaya avatar martin-rueegg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

geonames-client's Issues

SSL certificate problem

Hi,

I get this exception thrown since today when I try to create a new client.
cURL error 60: SSL certificate problem: certificate has expired (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

Honestly I don't know what to do with it, my SSL certificates are ok.
What's strange is that on a another project which is on the same server (just another subdomain, also with a Let's encrypt certificate) the Problem didn't appear.

Did someone have a clue what could cause it?

SSL certificate problem: certificate has expired

Hi guys,
At first, thank you for your excellent work. Unfortunately I get the error below, when using this extension:

GuzzleHttp\Exception\RequestException

cURL error 60: SSL certificate problem: certificate has expired (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://secure.geonames.org/countryInfoJSON?lang=ru&username=books_bvks

I've updated the extension to the latest version and now I have an updated cacert.pem, but this didn't help me, and the error is still there.

Any ideas?

Identic parameters keys in query not work

Hi,

Need

Call geoname search to multiple countries.

Sample :
http://api.geonames.org/search?name_startsWith=Lond&country=FR&country=GB&featureClass=P&....

This request is supported by GeoName API ;)

Problem

Search query builder paramsToQueryString use php array key to map query parameter name.
By definition php array key are unique.
So add 2 identic country parameter name is not supported

Solution

Add support sequential array (no association : without key) in value parameters and to this case just reuse parent name in recursive call.

Sample worked and functional :

    /**
     * Convert Parameters Array to a Query String.
     *
     * Escapes values according to RFC 1738.
     *
     * @see http://forum.geonames.org/gforum/posts/list/8.page
     * @see rawurlencode()
     *
     * @param array $params Associative array of query parameters.
     *
     * @return string The query string.
     */
    protected function paramsToQueryString(array $params = []): string
    {
        $arrayIsAssoc = function (array $array) {
            return count(array_filter(array_keys($array), 'is_string')) > 0;
        };

        $query_string = [];
        foreach ($params as $name => $value) {
            if (empty($name)) {
                continue;
            }
            if (is_array($value)) {
                // recursion case

                if ($arrayIsAssoc($value)) {
                    $result_string = $this->paramsToQueryString($value);

                    if (!empty($result_string)) {
                        $query_string[] = $result_string;
                    }
                } else {
                    foreach ($value as $subValue) {
                        $result_string = $this->paramsToQueryString([$name => $subValue]);

                        if (!empty($result_string)) {
                            $query_string[] = $result_string;
                        }
                    }
                }

                $result_string = $this->paramsToQueryString($value);
                if (!empty($result_string)) {
                    $query_string[] = $result_string;
                }
            } else {
                // base case

                $value = (string)$value;
                $query_string[] = $name . '=' . rawurlencode($value);
            }
        }

        return implode('&', $query_string);
    }

Wrong Base URL for Premium usage

When using the Premium Data plan the Base URL should be https://secure.geonames.net instead of https://secure.geonames.org.

This information came from the email when purchasing a Premium plan

We have enabled your account and you can access the premium server with the domain ws.geonames.net and the ssl domain is https://secure.geonames.net (.net instead of .org for the free server)

This seems to be the case for me as well since I seem to have hit the credits cap on the .org domain while .net works.

There's currently no way to properly override this. I suggest that we either have an additional parameter in the constructor, or move the base url to a config variable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.