【发布时间】:2015-07-06 18:06:31
【问题描述】:
我已经在本地主机上成功地在我的网站中实现了地理位置。但是,将网站上传到在线服务器后,它就不再起作用了。我得到一个 bool(false) 错误。
Geo.php
<?php
class Geo{
protected $api = 'http://www.telize.com/geoip/%s';
protected $properties = [];
public function __get($key){
if (isset($this->properties[$key])){
return $this->properties[$key];
}
return null;
}
public function request($ip){
$url = sprintf($this->api, $ip);
$data = $this->sendRequest($url);
$this->properties = json_decode($data, true);
}
protected function sendRequest($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
return curl_exec($curl);
}
}
?>
index.php
<?php
include ("functions/functions.php");
$ip = getIp();
require 'Geo.php';
$geo = new Geo;
$geo->request('$ip');
$location = $geo->city . ', ' .$geo->region;
?>
getIp() 函数位于我包含的 function.php 文件中,它工作正常,因此我将其排除在外。
【问题讨论】:
-
你在哪一行得到错误?
-
@jcuenod,它只是回显:bool(false),没有说什么行。
-
请注意,
$geo->request('$ip');将传递字符串 $ip 而不是该变量的值。请改用$geo->request($ip); -
@Stefan,还是不行
-
有更好的方法吗?
标签: php jquery html geolocation