Use thinkphp to determine the geographical location based on the user's ip address and provide the corresponding weather information

Source: Internet
Author: User
Based on thinkphp, you can determine the geographical location based on your ip address and provide the corresponding weather information. Thinkphp-based applications that determine geographical locations based on users' ip addresses and provide corresponding weather information. we all know that many websites provide users with the weather forecast function, sometimes thinkphp is used to determine geographical locations based on users' ip addresses and provide corresponding weather information.

We all know that many websites provide weather forecasts for users. sometimes, even if users do not enter any geographical location information related to themselves, the same website can also determine the geographic location and display the weather information. we may think of using ip addresses to determine the geographic location. this is indeed the case. However, if you want to develop your own services completely, it is indeed a little troublesome, so here we will briefly talk about the business logic of this application (starting from the first time you open the page ):

1. ip address acquisition: There are two types of ip addresses, one is an independent ip address, which actually exists, and the other is the ip address assigned by the router, of course, this is not unique in a certain sense. of course, you must consider the two ip addresses. The first one is very easy or easy to obtain. you can obtain the element value in $ _ SERVER, however, the problem is the second type. if something like 192.168.1.1 cannot be determined, the user's location information cannot be processed or obtained. here, the curl extension is used to capture the information, however, this extension cannot be used. Therefore, you can only use an independent ip address to access this extension.

Here, we will add that the server parameter is not server_addr but REMOTE_ADDR. Only in this way can we get the real address of the user. in this way, we don't have to bother with additional processing.

2: After we get the ip address, we should find the ip address location of the user based on the ip address Library. here there are two areas to note: 1: The ip address library must be UTF-8 encoded, 2: or the obtained information is a complete address information, which must be followed by the city name board. Therefore, you need to take a string or simplify the City address.

3, here I used a brute force method to traverse the city code library or get the city code.

4: after obtaining the city code, you need to connect to the data interface to obtain the data. json data requires json_encode () transcoding. here I have an object, some interfaces are different. the specific situation is analyzed. then, the most important thing is to allocate the data, which is actually the object. later, it was found that the allocation was correct, this facilitates code migration.

5: Call the data allocated by the template in the view. I believe this will happen.

6: In fact, the entire business logic of the first page opened has been completed in the above steps. another one is that in the view, I provide the user with the function of entering the city name to query the city weather, in this way, the city name is used to obtain the user input information.

7: Now the city name has been obtained. you can directly retrieve the city code and traverse it, here, a very important logic problem is how to combine the two functions in sequence and how to judge them. it must be:

First, determine whether the system automatically obtains whether the city name is null. if it is not empty, only the city name is or the name. Otherwise, the system prompts that the corresponding address information is not found for the IP address. then, note that it must be followed by the following sequence structure to determine whether the user input is empty. if it is not empty, let the city name be the user input, all in all, the user's input permissions must be greater than the permissions automatically obtained by the system, so that the two codes can be combined completely. In addition, I used two methods to obtain the ip address and retrieve the city name respectively.

The above is a mess. in this case, some of my instance code is shown below. I cannot use the data interface because of various security copyright reasons. I only provide method reference, paste useless, independent research.

As shown in weather_test.html:

    

Real-time weather information
City {$ All_info-> forecast-> city}
Basic Weather {$ All_info-> realtime-> weather}
Temperature {$ All_info-> realtime-> temp}
Wind direction {$ All_info-> realtime-> WD}
Update Time {$ All_info-> realtime-> time}
Life suggestions
City {$ All_info-> forecast-> city}
Anti-renewal suggestions {$ All_info-> index [0]-> details}
Recommended clothes {$ All_info-> index [1]-> details}
Exercise suggestions {$ All_info-> index [2]-> details}
Car wash suggestions {$ All_info-> index [3]-> details}
Drying suggestions {$ All_info-> index [4]-> details}
Update Time {$ All_info-> realtime-> time}
Weather information city for the next four days: {$ all_info-> forecast-> city}
Project/Date Today Tomorrow The Day After Tomorrow Dayu
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 direction {$ All_info-> forecast-> wind1} {$ All_info-> forecast-> wind2} {$ All_info-> forecast-> wind3} {$ All_info-> forecast-> wind4}
Wind power {$ All_info-> forecast-> fl1} {$ All_info-> forecast-> fl2} {$ All_info-> forecast-> fl3} {$ All_info-> forecast-> fl4}
Air quality 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 '; // determine the location based on the client ip address. // define two variable $ count = 0; $ city_id = '2016 '; // The default value is $ city_name_cin =$ _ POST ['cityname']; $ city_sim_name = R ('test/get_user_cityname '); // This is the location automatically determined by the system based on the ip address echo $ city_sim_name; if ($ city_sim_name! = Null) {$ city_name_cin = $ city_sim_name;} // This is the user input location if (! Empty ($ _ POST ['cityname']) {// Place information overwrite $ city_name_cin =$ _ POST ['cityname'];} ////// no matter whether it is ip location or user input, the final variable that needs to be traversed to obtain the city code is as long as $ city_name_cinforeach ($ citycode as $ key => $ value) {if ($ key = $ city_name_cin) {$ city_id = $ citycode [$ city_name_cin]; $ count ++ ;}} if ($ count = 0) {echo sorry, the address you entered cannot be found! The default is Zibo.} the weather information of else {echo $ city_name_cin. is as follows;} // The interface has been processed. please do not use it. it is only for learning $ weather_interface_url = http://weatherai.markt.xiaomi.com/wtr-v2/weather?cityId= . $ City_id. & mei = require & device = HM2013023 & miuiVersion = JHBCNBD16.0 & modDevice = ce = miuiWeatherAp; $ all_weather_info = json_decode (file_get_contents ($ weather_interface_url )); // allocate data $ this-> assign (all_info, $ all_weather_info); $ this-> display ();} //////////////////////////////////////// ///// // ** the following two methods are used to obtain the host ip address and the ip address location, the final result value is obtained through the R method. * // Method 1: Get the user's ippublic function get_user_ip () {// first obtain the host's ip address using this simple method, get $ host_ip = $ _ SERVER ['server _ ADDR '] through the R method;
Return $ host_ip;} // Method 2: obtain the public function get_user_cityname () {// obtain the user's ip address $ host_ip = R ('test/get_user_ip '); // Here, the Ip address library must be instantiated as $ Ip = new OrgNetIpLocation ('utfwry. dat '); // The instantiation class parameter indicates the ip address library file // you can put the Ip address and domain name $ area = $ ip-> getlocation ($ host_ip) at the same time ); // Obtain the location of the domain name server $ city_allname = $ area ['country']; $ sim_cityname = explode (city, explode (Province, $ city_allname) [1]) [0]; return $ sim_cityname ;}


We all know that many websites provide users with the weather forecast function, sometimes...

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: info-contact@alibabacloud.com 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.