PHP Classes

File: Logaty/Helpers/Detect/Country.php

Recommend this page to a friend!
  Classes of mohammad anzawi   PHP Multilingual Support Library   Logaty/Helpers/Detect/Country.php   Download  
File: Logaty/Helpers/Detect/Country.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Multilingual Support Library
Translate texts for Web sites from JSON or DB
Author: By
Last change:
Date: 3 years ago
Size: 1,489 bytes
 

Contents

Class file image Download
<?php

namespace PHPtricks\Logaty\Helpers\Detect;

trait
Country
{
    public function
country($code = '')
    {
        if(!
$code) $code = $this->getCountryCodeFromIp();
       
$countryInfo = logaty()->defaultLang();
        if(
false !== ($countryFile = @file_get_contents("https://restcountries.eu/rest/v1/alpha/{$code}")))
           
$countryInfo = json_decode($countryFile, true);
        return
is_array($countryInfo) ? $countryInfo['languages'][0] : $countryInfo;
    }
    private function
getCountryCodeFromIp()
    {
       
$ip = $this->getIp();
       
$countryCode = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $ip));
        return
strtolower($countryCode['geoplugin_countryCode']);
    }
    private function
getIp()
    {
       
$ipAddress = 'UNKNOWN';
        if(isset(
$_SERVER['HTTP_CF_CONNECTING_IP'])) {
           
$ipAddress = $_SERVER['HTTP_CF_CONNECTING_IP'];
        } else if(isset(
$_SERVER['HTTP_X_REAL_IP'])) {
           
$ipAddress = $_SERVER['HTTP_X_REAL_IP'];
        } else if(isset(
$_SERVER['HTTP_CLIENT_IP']))
           
$ipAddress = $_SERVER['HTTP_CLIENT_IP'];
        else if(isset(
$_SERVER['HTTP_X_FORWARDED_FOR']))
           
$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        else if(isset(
$_SERVER['HTTP_X_FORWARDED']))
           
$ipAddress = $_SERVER['HTTP_X_FORWARDED'];
        else if(isset(
$_SERVER['HTTP_FORWARDED_FOR']))
           
$ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
        else if(isset(
$_SERVER['HTTP_FORWARDED']))
           
$ipAddress = $_SERVER['HTTP_FORWARDED'];
        else if(isset(
$_SERVER['REMOTE_ADDR']))
           
$ipAddress = $_SERVER['REMOTE_ADDR'];
        return
$ipAddress;
    }

}