Xml 如何从ipaddress检索天气信息
我在Yii+extjs工作。我正在创建天气模块。我想从用户的IP地址检索天气信息。我从网站获得了参考代码=”http://www.geoplugin.com/examples". 正如上面提到的,我已经创建了getweather函数=Xml 如何从ipaddress检索天气信息,xml,yii,weather-api,Xml,Yii,Weather Api,我在Yii+extjs工作。我正在创建天气模块。我想从用户的IP地址检索天气信息。我从网站获得了参考代码=”http://www.geoplugin.com/examples". 正如上面提到的,我已经创建了getweather函数= public function actionGetWeather() { $user_ip = $_SERVER['REMOTE_ADDR']; //The Dat
public function actionGetWeather()
{
$user_ip = $_SERVER['REMOTE_ADDR'];
//The Data Science Toolkit URL
$url = 'http://www.datasciencetoolkit.org/ip2coordinates/';
//Find the user's location from their IP.
//*** You need the get_data function from the sample code
$raw_geocode = json_decode( get_data( $url . $user_ip) );
//Check if the user is in the US
if ('US' === $raw_geocode->$user_ip->country_code) {
//If yes, store their zip code in a variable, and print it
$zip_code = $raw_geocode->$user_ip->postal_code;
printf('<p>Your zip code is: %s</p>', $raw_geocode->$user_ip->postal_code);
} else {
//If the user isn't in the US, set a sip code that will work.
$zip_code = '97211';
//and print an error
printf('<p>Sorry, this app does not work in %s.</p>', $raw_geocode->$user_ip->country_name);
}
//Print the raw data for debugging.
printf('<pre>%s</pre>', print_r($raw_geocode, true));
}
公共函数actionGetWeather()
{
$user\u ip=$\u SERVER['REMOTE\u ADDR'];
//数据科学工具包URL
$url='1http://www.datasciencetoolkit.org/ip2coordinates/';
//从用户的IP查找用户的位置。
//***您需要使用示例代码中的get_data函数
$raw_geocode=json_decode(获取数据($url.$user_ip));
//检查用户是否在美国
如果('US'==$raw\U地理编码->$user\U ip->国家代码){
//如果是,将他们的邮政编码存储在变量中,并打印出来
$zip\u code=$raw\u geocode->$user\u ip->邮政编码;
printf(“您的邮政编码是:%s”,$raw\u geocode->$user\u ip->postal\u code);
}否则{
//如果用户不在美国,请设置一个可以工作的sip代码。
$zip_代码='97211';
//并打印一个错误
printf(“对不起,此应用程序在%s中无法运行。”,$raw\u geocode->$user\u ip->country\u name);
}
//打印原始数据以进行调试。
printf(“%s”,打印r($raw\u地理代码,true));
}
我的项目中也包含了ParseXml类。
但上面的代码给出的错误是致命错误:在getweather函数的“$raw_geocode=json_decode(get_data($url.$user_ip));”行调用未定义的函数get_data()。那么我需要什么样的改变呢?如果您正在使用curl,请帮助我,将get_data()的定义包括为 或者,简单地替换
$raw_geocode = json_decode(file_get_contents( $url . $user_ip) );
与
不要发布所有这些无关的代码,只需关注“真正的问题”,即
get\u data
未定义。。
$raw_geocode = json_decode( get_data( $url . $user_ip) );
$raw_geocode = json_decode(file_get_contents( $url . $user_ip) );