Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use my service, the <a href="http://ipinfo.io" rel="nofollow">http://ipinfo.io</a> API to get the country code:</p> <pre><code>function get_country($ip) { return file_get_contents("http://ipinfo.io/{$ip}/country"); } echo get_country("8.8.8.8"); // =&gt; US </code></pre> <p>If you're interested in other details you could make a more generic function:</p> <pre><code>function ip_details($ip) { $json = file_get_contents("http://ipinfo.io/{$ip}"); $details = json_decode($json); return $details; } $details = ip_details("8.8.8.8"); echo $details-&gt;city; // =&gt; Mountain View echo $details-&gt;country; // =&gt; US echo $details-&gt;org; // =&gt; AS15169 Google Inc. echo $details-&gt;hostname; // =&gt; google-public-dns-a.google.com </code></pre> <p>I've used the IP <code>8.8.8.8</code> in these examples, but if you want details for the user's IP just pass in <code>$_SERVER['REMOTE_ADDR']</code> instead. More details are available at <a href="http://ipinfo.io/developers" rel="nofollow">http://ipinfo.io/developers</a></p> <p>You can get a mapping of country codes to currency codes from <a href="http://country.io/data/" rel="nofollow">http://country.io/data/</a> and add that to your code. Here's a simple example:</p> <pre><code>function getCurrenyCode($country_code) { $currency_codes = array( 'GB' =&gt; 'GBP', 'FR' =&gt; 'EUR', 'DE' =&gt; 'EUR', 'IT' =&gt; 'EUR', ); if(isset($currency_codes[$country_code])) { return $curreny_codes[$country_code]; } return 'USD'; // Default to USD } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload