Based on thinkphp, it can be 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! 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 = 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 ;}