【发布时间】:2018-09-17 18:40:18
【问题描述】:
尝试从 whoisxmlapi 获取 IP 地理定位以工作。当我使用他们提供的示例代码时,一切正常。这是我使用的代码。
<?php
$ip = $_SERVER["HTTP_X_REAL_IP"];
$api_key = 'your_api_key';
$api_url = 'https://geoipify.whoisxmlapi.com/api/v1';
$url = "{$api_url}?apiKey={$api_key}&ipAddress={$ip}";
print(file_get_contents($url));
?>
这是我打印在页面上的输出。
{"ip":"174.222.131.171","location":{"country":"US","region":"California","city":"Fresno","lat":36.74773,"lng":-119.77237,"postalCode":"93650","timezone":"-07:00"}}
然后,当我尝试仅使用以下代码回显区域时,页面上绝对没有任何内容
$url = "{$api_url}?apiKey={$api_key}&ipAddress={$ip}";
$ipfeed = json_decode($url);
echo $ipfeed->region;
更新: 错误日志记录
[2018 年 9 月 17 日星期一 18:20:01.101218] [proxy_fcgi:error] [pid 6803] [client 127.0.0.1:34987] AH01071:出现错误“PHP 消息:PHP 警告:非法字符串偏移量“区域”在 / home/url.com/app/public_html/wp-content/plugins/custom-ads-plugin/custom-ads.php 在第 191 行\nPHP 消息:PHP 警告:file_get_contents(h):打开流失败:没有这样的文件或 /home/url.com/app/public_html/wp-content/plugins/custom-ads-plugin/custom-ads.php 中的目录在第 191 行\nPHP 消息:PHP 警告:/home 中的非法字符串偏移“区域” /url.com/app/public_html/wp-content/plugins/custom-ads-plugin/custom-ads.php 在第 191 行\nPHP 消息:PHP 警告:file_get_contents(h):打开流失败:没有这样的文件或/home/url.com/app/public_html/wp-content/plugins/custom-ads-plugin/custom-ads.php 中的目录在第 191 行\n'
【问题讨论】:
-
Region 位于另一个名为 location 的对象中。试试
$ipfeed->location->region; -
不。没用。