Mpociot\VatCalculator\VatCalculator::getIPBasedCountry PHP Method

getIPBasedCountry() public method

Returns the ISO 3166-1 alpha-2 two letter country code for the client IP. If the IP can't be resolved it returns false.
public getIPBasedCountry ( ) : boolean | string
return boolean | string
    public function getIPBasedCountry()
    {
        $ip = $this->getClientIP();
        $url = self::GEOCODE_SERVICE_URL . $ip;
        $result = file_get_contents($url);
        switch ($result[0]) {
            case '1':
                $data = explode(';', $result);
                return $data[1];
                break;
            default:
                return false;
        }
    }

Usage Example

Example #1
0
 /**
  * Returns the tax rate for the given country.
  *
  * @return \Illuminate\Http\Response
  */
 public function getCountryCode()
 {
     return ['country_code' => $this->calculator->getIPBasedCountry()];
 }