Thinkphp based on the application of the user IP to determine the geographical location and provide the corresponding weather information _php tutorial

Source: Internet
Author: User
Tags get ip

Based on the thinkphp, the application of the geographical location and the corresponding weather information according to the user IP

We all know that in a lot of sites provide users with the ability to provide weather forecasts, and sometimes found that users, even if they do not enter any information about their geographical location, the same site can be used to determine the geographical location and display the weather information, we may think of IP to determine the location, it is true, but, If you want to develop as well as you do, it's a bit of a hassle, so here's a brief look at the business logic of the application (starting with the first time the user opens the page):

1: Get Ip:ip According to my own statement there are two, one is the independent IP, real existence, the other is the router assigned IP, this certainly does not have a sense of uniqueness, the two IP of course to consider, the first one very easy or get, take $_server within the value of the element can be, But the problem is the second, like such is not able to judge, then can not process or get the user's location information, here is used to crawl the expansion of curl, but this extension said what is not used, so this situation first put aside, only consider the user use of independent IP access.

Here to add, the server parameter is not server_addr but remote_addr, only in this way to get the user's true address, so that you do not have trouble to take another deal

2: When we acquire the IP should be based on the IP address library to find the user's IP location, there 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 office can, so, to do the string interception, Or to simplify the city address.

3: When or get the user's address obviously still do not, think, finally in the weather interface docking to obtain data is the city code, these two still want to convert, here I used a violent way to traverse the city code base or get the city code.

4: Get to the city code, will go to connect the data interface to get the data, the data when the JSON data, the need to Json_encode () transcoding, I am here or get an object, some interface is not the same, the specific situation of specific analysis, and then, the most important thing is to allocate these data, said is these, This is actually the object, and later found that no allocation is correct, which facilitates the porting of code.

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

6: In fact, the above steps have to open the first page of the entire business logic to deal with, there is a view to the user provides the input city name Query City weather function, so in order to analyze, normal access to the user's input information is the city name

7: Here has already or has the city name, directly participates in obtains the city code the traversal to be possible, here has a very important logic question is the order and how to judge only then can the two function very good unifies in one, must be:

First determine whether the system automatically get the city name is empty, if not empty city name is or get the name, conversely, the prompt IP address does not query to the corresponding address information, followed by, attention must be immediately following, is the sequence 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 permissions must be greater than the system automatically get permissions, so you can complete the two code together, in addition, I was to obtain the IP and take out the city name respectively made two methods.

The above is written in a disorderly, so, below is my part of the code, because of security copyright reasons, data interface I did the processing can not be used, 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}
temp {$all _info->realtime->temp}
Wind Direction {$all _info->realtime->wd}
Update Time {$all _info->realtime->time}
Life Advice
City {$all _info->forecast->city}
Anti-sunscreens recommendations {$all _info->index[0]->details}
Dressing advice {$all _info->index[1]->details}
Sports Advice {$all _info->index[2]->details}
Car Wash Advice {$all _info->index[3]->details}
Drying advice {$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
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->FL2} {$all _info->forecast->fl3} {$all _info->forecast->fl4}
Air quality conditions 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 ';//////here is based on the IP of the client to determine the location//definition of two flag 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 based on IP automatically determine the location of 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 ')) {//placement information overrides $city_name_cin=$_post[' CityName '];} Whether IP location or user input eventually needs to traverse the variable that gets the city code 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 was not found! Default Zibo Oh;} Else{echo $city _name_cin Weather information is as follows;} Interface has been processed, 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));//Distribution 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, the final result value through the R method, return to get */// Method 1: Get the user Ippublic function get_user_ip () {//first obtain the host's IP through this simple method, obtain $host_ip=$_server[' server_addr ' by means of the R method;
return $host _ip;} Method 2: Get the place name Public function Get_user_cityname () {//Get the user's ip$host_ip=r (' test/get_user_ip ');//Here the IP address library must instantiate this $IP = new Orgnetiplocation (' UTFWry.dat '); The instantiation class parameter represents the IP address library file//can be simultaneously placed IP and domain name $area = $Ip->getlocation ($host _ip); Get the name server location $city_allname= $area [' Country ']; $sim _cityname=explode (City, Explode (province, $city _allname) [1]) [0];return $ Sim_cityname;} true techarticle based on the application of thinkphp to determine the geographic location and provide the weather information according to the user's IP we all know that in many sites there are features that provide weather forecasts to users, sometimes ...

  • 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.