Based on thinkphp, implement an application that determines the geographical location based on the user's IP and provides corresponding weather information_PHP tutorial

WBOY
Release: 2016-07-13 09:47:15
Original
759 people have browsed it

Based on thinkphp, an application is implemented to determine the geographical location based on the user’s IP and provide corresponding weather information

We all know that many websites provide users with the function of providing weather forecasts. Sometimes we find that even if users do not enter any geographical location information related to themselves, the same website can also determine the geographical location and display it. For weather information, we may think of using IP to determine the geographical location. This is indeed the case. However, if you want to develop it yourself, it is indeed a bit troublesome, so here is a brief talk about the business logic of this application (based on the user’s first time Open the page as the starting point):

1: Get IP: According to my own opinion, there are two types of IP. One is an independent IP, which really exists, and the other is an IP assigned by the router. This is of course not unique in a certain sense. These two Of course, all kinds of IP must be considered. The first one is very easy to obtain. Just take the element value in $_SERVER. But the problem is the second one. It cannot be judged like 192.168.1.1, so it cannot be processed or obtained. The user's location information was originally captured using the curl extension, but this extension cannot be used, so this situation is put aside for now and only the situation where the user uses an independent IP to access is considered.

I would like to add here that the parameter of the server is not server_addr but REMOTE_ADDR. Only in this way can the real address of the user be obtained. In this case, there is no need to go through the trouble of obtaining it separately

2: After we obtain the IP, we should find the user’s IP location based on the IP address database. There are two things to note here. 1: The IP address database must be utf-8 encoded. 2: The obtained information is A complete address information, but the city name bureau is needed later, so we need to intercept the string or simplify the city address.

3: It is obviously not possible to get the user's address. Think about it, it is the city code that is finally connected to the weather interface to obtain the data. These two still need to be converted. Here I used a violent method to traverse the city code library or Get the city code.

4: After getting the city code, you need to connect to the data interface to get the data. This data is json data and needs to be transcoded by json_encode(). What I get here may be an object, and some interfaces are different. Specifically After analyzing the situation in detail, the most important thing is to allocate these data. It is said that these are actually the objects. Later, it was found that it was correct not to allocate them one by one, which is conducive to code transplantation.

5: Call the data allocated by the template in the view, I believe this will be done.

6: In fact, the above steps have already processed the entire business logic of opening the page for the first time. Another thing is that I provide the user with the function of inputting the city name to query the city weather in the view, so I can analyze it in sequence. , the normal input information obtained by the user is the city name

7: The city name is already or obtained here. You can directly participate in the traversal to obtain the city code. There is a very important logical issue here, which is the order and how to judge to combine these two functions well. One piece, it must be:

First determine whether the system automatically obtains the city name and whether it is empty. If it is not empty, only the city name is the obtained name. Otherwise, it will prompt that the IP address has not found the corresponding address information. Next, please note that it must be the following. , is a sequential structure that determines whether the user's input is empty. If it is not empty, then let the user input the city name. In short, the user's input permissions must be greater than the permissions automatically obtained by the system, so that the two can be completely combined The codes are combined together. In addition, I made two methods to obtain the IP and extract the city name.

The above writing is a bit messy. In this case, here is some of my example code. Due to various security and copyright reasons, I have processed the data interface and cannot use it. It only provides method reference. Pasting is useless. I will research it independently.

View weather_test.html:

Copy after login

即时天气信息
城市 {$all_info->forecast->city}
基本天气 {$all_info->realtime->weather}
温度 {$all_info->realtime->temp}
风向 {$all_info->realtime->WD}
更新时间 {$all_info->realtime->time}
生活建议
城市 {$all_info->forecast->city}
防嗮建议 {$all_info->index[0]->details}
穿衣建议 {$all_info->index[1]->details}
运动建议 {$all_info->index[2]->details}
洗车建议 {$all_info->index[3]->details}
晾晒建议 {$all_info->index[4]->details}
更新时间 {$all_info->realtime->time}
未来四天天气信息  城市: {$all_info->forecast->city}
项目/日期 今天 明天 后天 大后天
概况 {$all_info->forecast->weather1} {$all_info->forecast->weather2} {$all_info->forecast->weather3} {$all_info->forecast->weather4}
温度 {$all_info->forecast->temp1} {$all_info->forecast->temp2} {$all_info->forecast->temp3} {$all_info->forecast->temp4}
风向 {$all_info->forecast->wind1} {$all_info->forecast->wind2} {$all_info->forecast->wind3} {$all_info->forecast->wind4}
风力 {$all_info->forecast->fl1} {$all_info->forecast->fl2} {$all_info->forecast->fl3} {$all_info->forecast->fl4}
今日空气质量状况
城市 {$all_info->forecast->city}
PM2.5 {$all_info->aqi->pm25}
PM10 {$all_info->aqi->pm10}
SO2 {$all_info->aqi->so2}
NO2 {$all_info->aqi->no2}
更新时间 {$all_info->aqi->pub_time}
类方法:

public function weather_test(){
		require_once './Component/Citycode.php';
		//////这里是根据客户端的ip判断地理位置
		//定义两个标志变量
		$count=0;
		$city_id='101120301';//默认大淄博
		$city_name_cin=$_POST['cityname'];
		$city_sim_name=R('Test/get_user_cityname');
		//这是系统根据ip自动判断的位置
		echo $city_sim_name;
		if($city_sim_name!=null){
			$city_name_cin=$city_sim_name;
		}
		//这是用户输入的位置
		if(!empty($_POST['cityname'])){
			//放置信息覆盖
			$city_name_cin=$_POST['cityname'];
		}
		////////不管是ip定位还是用户输入最终需要遍历获取城市代码的变量只要$city_name_cin
		foreach ($citycode as $key => $value){
			if($key==$city_name_cin){
				$city_id=$citycode[$city_name_cin];
				$count++;
			}
		}
		if($count==0){
			echo 对不起,您输入的地址没有找到!默认淄博哦;
		}else{
			echo $city_name_cin.的天气信息如下;
		}
		//接口已经处理,请勿使用,仅供学习
		$weather_interface_url=http://weatherai.markt.xiaomi.com/wtr-v2/weather?cityId=.$city_id.&mei=e32c88633283737f5d9f381d47&device=HM2013023&miuiVersion=JHBCNBD16.0&modDevice=ce=miuiWeatherAp;
		$all_weather_info = json_decode(file_get_contents($weather_interface_url));
		//分配数据
		$this->assign(all_info,$all_weather_info);
		$this->display();
	}
	///////////////////////////////////////////////////
	/*
	 * 下面两个分别是获取主机ip和ip所在地的两个
	 * 方法,最后的结果数值通过R方法,返回获取
	 */
	//方法1:获取用户ip
	public function get_user_ip(){
		//先通过这种简单的方法获取主机的ip,通过R方法获取
		$host_ip=$_SERVER['SERVER_ADDR'];
Copy after login
 		return $host_ip;
	}
	//方法2:获取地名
	public function get_user_cityname(){
		//得到用户的ip
		$host_ip=R('Test/get_user_ip');
		//这里ip地址库必须这样实例化
		$Ip = new OrgNetIpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
		//可以同时放ip和域名
		$area = $Ip->getlocation($host_ip); // 获取域名服务器所在的位置
		$city_allname=$area['country'];
		$sim_cityname=explode(市,explode(省, $city_allname)[1])[0];
		return $sim_cityname;
	}
Copy after login


 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1027128.htmlTechArticle基于thinkphp实现根据用户ip判断地理位置并提供对应天气信息的应用 我们都知道,在很多的网站都提供了给用户提供天气预报的功能,有时...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!