Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress非常简单的地理定位_Php_Json_Wordpress_Geolocation - Fatal编程技术网

Php WordPress非常简单的地理定位

Php WordPress非常简单的地理定位,php,json,wordpress,geolocation,Php,Json,Wordpress,Geolocation,我需要做一些非常基本的地理定位为WordPress网站 比如: <?php if ( visitor is from usa ): ?> <?php get_footer('us'); ?> <?php else: ?> <?php get_footer('other-countries'); ?> <?php endif; ?> 但我仍然需要帮助来整理最终的完整代码。试试这个: <?php $ip = ( ! empty( $

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

比如:

<?php if ( visitor is from usa ): ?>
<?php get_footer('us'); ?>
<?php else: ?>
<?php get_footer('other-countries'); ?>
<?php endif; ?>
但我仍然需要帮助来整理最终的完整代码。

试试这个:

<?php
$ip = ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) ?
    $_SERVER['REMOTE_ADDR'] : '';

$country_code = '';
if ( $ip ) {
    // 'fields' are comma-separated list of fields to include in the response.
    // No spaces between the fields. See http://ip-api.com/docs/api:returned_values#field_generator
    $url = 'http://ip-api.com/json/' . $ip . '?fields=countryCode,status,message';

    $res_body = wp_remote_retrieve_body( wp_safe_remote_get( $url ) );
    $geo = json_decode( $res_body );

    $country_code = isset( $geo->countryCode ) ? $geo->countryCode : '';
}

if ( 'US' === $country_code ) : ?>
    DO THE THIS
<?php else : ?>
    DO THE THAT
<?php endif; ?>

做这个
做那件事
附言:我刚学会我应该使用代码

是的,这是使用jQuery的一个例子

$.getJSON( '//ip-api.com/json?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});
<?php
$ip = ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) ?
    $_SERVER['REMOTE_ADDR'] : '';

$country_code = '';
if ( $ip ) {
    // 'fields' are comma-separated list of fields to include in the response.
    // No spaces between the fields. See http://ip-api.com/docs/api:returned_values#field_generator
    $url = 'http://ip-api.com/json/' . $ip . '?fields=countryCode,status,message';

    $res_body = wp_remote_retrieve_body( wp_safe_remote_get( $url ) );
    $geo = json_decode( $res_body );

    $country_code = isset( $geo->countryCode ) ? $geo->countryCode : '';
}

if ( 'US' === $country_code ) : ?>
    DO THE THIS
<?php else : ?>
    DO THE THAT
<?php endif; ?>