我的功能基本上返回访问者的国家代码和另一个获取货币的功能。在Windows上运行XAMPP时,函数的返回值会像预期的那样回显。 在Linux(eOS Loki)上,我手动安装了LAMPP堆栈(没有运行XAMPP),这次PHP不会回显函数的返回值。该页面将停止加载,没有错误。
page stops loading immediately
注意:nhazi.dev是localhost
国家/地区代码功能
function get_ipinfo( $option )
{
$url = "http://ipinfo.io/" . $_SERVER['REMOTE_ADDR'] . "/json";
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1') // I develop offline most times.
{
$url = "http://nhazi.dev/php_curl/json"; //Offline Debugging
}
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true
)
);
$data = json_decode( curl_exec($ch) ); // decodes json response
curl_close($ch);
// Return value based on option
if(! $option)
return $data;
if($option == 'ip')
return $data->ip;
if($option == 'city')
return $data->city;
if($option == 'region')
return $data->region;
if($option == 'country')
return $data->country;
if($option == 'location')
return $data->loc;
if($option == 'org')
return $data->org;
}
货币代码功能
function get_currency( $countrycode )
{
$url = "http://country.io/currency.json";
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
{
$url = "http://nhazi.dev/php_curl/currency"; //Offline Debugging
}
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
)
);
$currency = json_decode(curl_exec($ch));
// print_r($currency); // Debugging
curl_close($ch);
return $currency->$countrycode;
}
功能实施
<p>
<?php echo get_currency( get_ipinfo('country') ); ?>
</p>