Duke Yin's Technology database

WordPress通过IP获得地理位置

<?php
if (!empty($_SERVER['HTTP_CLIENT_IP']))   
{
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
{
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip_address = $_SERVER['REMOTE_ADDR'];
}
$content = wp_remote_retrieve_body( wp_remote_get('https://apis.map.qq.com/ws/location/v1/ip?ip='.$ip_address.'&key='.get_option( 'p2_qqmap_key' ) ) );
$content_obj = json_decode($content, true);
print_r($content_obj);
$region_id = $content_obj['result']['ad_info']['adcode'];
$region_ct = $content_obj['result']['ad_info']['city'];
echo $region_id;
echo $region_ct;
?>

# #

引用关闭,但你能发布评论.

评论