使用JSON PHP进行简单的地理定位 [英] Simple geolocation with JSON PHP

查看:82
本文介绍了使用JSON PHP进行简单的地理定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为WordPress网站做一些非常基本的地理定位。



类似于:



<?php if(访客来自美国):?>

<?php get_footer('us'); ?>

<?php else:?>

<?php get_footer('other-countries'); ?>

<?php endif; ?>



到目前为止我使用了ip-api.com提供的GEO服务。你可以在下面看到我以前使用过的代码。



问题是php unserialize现在已被弃用且非常慢。 Ip-api建议改为使用JSON 。但我是一个新手,我不知道如何用JSON实现相同的结果。有人可以帮帮我吗?



我知道我应该使用以下代码:

 $。getJSON ('//ip-api.com/json?callback=?',function(data){
console.log(JSON.stringify(data,null,2));
});



但我需要帮助来形成完整的最终代码。



我知道有各种各样的插件在那里,但我认为他们对于我需要的非常简单的地理定位来说太过分了。



我尝试了什么:



<?php $ ip = $ _SERVER ['REMOTE_ADDR']; 
$ query = @unserialize(file_get_contents('http://ip-api.com/php/'。$ ip));
$ geo = $ query ['countryCode'];
if($ query ['countryCode'] =='US'):?>
DO THUS
<?php else:?>
DO $ $ $ b<?php endif?>

解决方案

.getJSON('// ip- api.com/json?callback=?',function(data){
console.log(JSON.stringify(data,null,2));
});



但我需要帮助来形成完整的最终代码。



我知道有各种各样的插件,但我认为他们对于我需要的非常简单的地理定位来说是过度的。



我尝试过的事情:



< PHP 

IP =

_SERVER [ 'REMOTE_ADDR'];

I need to do some very basic geo-targeting for a WordPress site.

Something like:

<?php if ( visitor is from usa ): ?>
<?php get_footer('us'); ?>
<?php else: ?>
<?php get_footer('other-countries'); ?>
<?php endif; ?>

Till now I used the GEO service provided by ip-api.com. You can see the code I used till now, below.

The problem is that the php unserialize is now deprecated and extremely slow. Ip-api suggest to use JSON instead. But I am a total newbie and I don't know how to achieve the same results with JSON. Can someone please help me?

I do know that I should use the following code:

$.getJSON( '//ip-api.com/json?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});


But I need help to form the complete final code.

I know there are various plug-ins out there, but I think they are overkill for the very simple geo targeting I need.

What I have tried:

<?php $ip = $_SERVER['REMOTE_ADDR'];
$query = @unserialize(file_get_contents('http://ip-api.com/php/' . $ip));
$geo = $query['countryCode'];
if( $query['countryCode'] == 'US') : ?>
                    DO THIS
<?php else: ?>
                    DO THAT
<?php endif ?>

解决方案

.getJSON( '//ip-api.com/json?callback=?', function( data ) { console.log( JSON.stringify( data, null, 2 ) ); });


But I need help to form the complete final code.

I know there are various plug-ins out there, but I think they are overkill for the very simple geo targeting I need.

What I have tried:

<?php


ip =


_SERVER['REMOTE_ADDR'];


这篇关于使用JSON PHP进行简单的地理定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆