Based on thinkphp to judge geographical location according to user's IP and provide corresponding weather information application

Source: Internet
Author: User
Tags object empty explode final get ip interface variable access

We all know that many websites offer the ability to provide weather forecasts to users, and sometimes find that the same site can judge geographic location and display weather information, even if the user does not enter any information about their geographic location, and we may think of IP to judge the location, but It's a bit of a hassle if you want to develop your own as well, so here's a quick way to say the business logic of this application (starting with the first time a user opens the page):

1: Get Ip:ip According to my own saying there are two kinds of one kind is the independent IP, the real existence, another kind is the IP which the router assigns, this certainly does not have the certain sense the uniqueness, these two kinds of IP certainly must consider, the first kind is very easy or obtain, takes the $_server element value to be possible, But the problem is the second kind, such as is impossible to judge, then can not handle or access to the user's location information, where the original use of curl expansion to crawl, but this extension said what is not available, so this situation first put aside, consider only the user using independent IP access.

Here to add, the server's parameters are not server_addr but remote_addr, only in order to get the user's real address, so that you do not bother to take additional processing

2: When we get the IP should be based on the IP address library to find the user's IP location, here are two places to note that the 1:IP address library must be UTF-8 encoded, 2: or the information is a complete address information, and the following need city name Bureau can, so, to do a string intercept, Or to simplify the city address.

3: When the user's address is clearly still not, think, the final weather interface to obtain data is the city code, these two still want to transform, here I used a violent way to traverse the city code base or get the city code.

4: After getting to the code of the city, to connect to the data interface to get the data, this data JSON data, need Json_encode () transcoding, I here or have an object, some interface is not the same, concrete analysis, and then, the most important thing is to allocate these data, said is these, This is actually the object, and it turns out that not being assigned is the right one, which is good for code porting.

5: In the view to call the template allocation of data, I believe this will.

6: In fact, the above steps have been the first open page of the entire business logic to deal with, there is another one is I in the view of the user to provide input to the city to query the city weather function, so in order to analyze, the normal access to the user's input information is the city name

7: Here has been or got the city name, directly involved in obtaining the city code of the traversal can be, here is a very important logical question is the sequence and how to judge to be able to put these two functions well together in a piece, must be:

First determine whether the system automatically gets the city name is empty, if not empty only city name is or get the name, on the contrary, prompted the IP address did not query to the corresponding address information, followed by, attention must be immediately following, is the order of structure, to determine whether the user's input is empty, if not empty, then let the city name for the user input , in a word, the user's input permission must be greater than the system automatically obtain permissions, so that you can complete the two code together, in addition, I was to get the IP and take out the name of the city made two separate methods.

Written above the more chaotic, so, the following is part of my example code, because of security copyright in all aspects of reasons, data interface I did not use the processing, only provide method reference, paste useless, independent research.

View weather_test.html:

Instant Weather Information
City {$all _info->forecast->city}
Basic weather {$all _info->realtime->weather}
Temperature {$all _info->realtime->temp}
Wind {$all _INFO->REALTIME->WD}
Update Time {$all _info->realtime->time}
Life Advice
City {$all _info->forecast->city}
Anti-Sunscreen Suggestions {$all _info->index[0]->details}
Dressing suggestions {$all _info->index[1]->details}
Sports Advice {$all _info->index[2]->details}
Car Wash Suggestion {$all _info->index[3]->details}
Drying Suggestions {$all _info->index[4]->details}
Update Time {$all _info->realtime->time}
Weather information for the next four days city: {$all _info->forecast->city}
Project/Date Today Tomorrow Acquired Big Day After Tomorrow
Overview {$all _info->forecast->weather1} {$all _info->forecast->weather2} {$all _info->forecast->weather3} {$all _info->forecast->weather4}
Temperature {$all _INFO->FORECAST->TEMP1} {$all _INFO->FORECAST->TEMP2} {$all _info->forecast->temp3} {$all _info->forecast->temp4}
Wind {$all _info->forecast->wind1} {$all _info->forecast->wind2} {$all _info->forecast->wind3} {$all _info->forecast->wind4}
wind {$all _info->forecast->fl1 {$all _info->forecast->fl3} {$all _info->forecast->fl4}
Air Quality Status today
City {$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}
Update Time {$all _info->aqi->pub_time}
Class method:
Public Function Weather_test () {require_once './component/citycode.php ';
		This is based on the client's IP location//Definition Two flags variable $count = 0;
		$city _id= ' 101120301 ';//default large Zibo $city _name_cin=$_post[' CityName '];
		$city _sim_name=r (' test/get_user_cityname ');
		This is the system according to the position of IP automatic judge Echo $city _sim_name;
		if ($city _sim_name!=null) {$city _name_cin= $city _sim_name;
		//This is the user-entered position if (!empty ($_post[' CityName ')) {//placement information covers $city _name_cin=$_post[' CityName ']; ////////whether IP positioning or user input ultimately requires traversing a variable to get the city code as long as $city_name_cin foreach ($citycode as $key => $value) {if ($key = = $city _nam
				e_cin) {$city _id= $citycode [$city _name_cin];
			$count + +; } if ($count ==0) {echo Sorry, the address you entered is not found!
		The default Zibo oh;
		}else{Echo $city _name_cin. The weather information is as follows; }//interface has been processed, please do not use, only for learning $weather _interface_url= $city _id. &mei=e32c88633283737f5d9f381d47&device=hm2013023&miuiversion=jhbcnbd16.0&moddevice=ce=
		Miuiweatherap; $all _weather_info = Json_decode (fiLe_get_contents ($weather _interface_url));
		Allocation Data $this->assign (All_info, $all _weather_info);
	$this->display (); ////////////////////////////////////////////////////* The following two are the two * methods to obtain the host IP and IP location, and the final result value is obtained by R method, and returns the Get//Method 1 : Gets the User IP public function get_user_ip () {//First obtains the host's IP through this simple method, obtains $host _ip=$_server[' Server_addr ' by the R method;
 		return $host _ip;
	Method 2: Obtain
	the name of the public Function Get_user_cityname () {
		//Get the user's IP
		$host _ip=r (' test/get_user_ip ');
		Here the IP address library must be instantiated
		$Ip = new Orgnetiplocation (' UTFWry.dat ');//Instantiate class parameters represent IP address library files
		//can place both IP and domain
		$area = $Ip- >getlocation ($host _ip); Get the location of the domain name server
		$city _allname= $area [' Country '];
		$sim _cityname=explode (city, Explode (province, $city _allname) [1]) [0];
		return $sim _cityname;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.