PHP interface get public IP and get weather interface information

Source: Internet
Author: User
Tags curl

<?PHPfunctionget_ip () {//determine if the server allows $_server        if(isset($_server)){                if(isset($_server[' Http_x_forwarded_for '])){                $realip=$_server[' Http_x_forwarded_for ']; }ElseIf(isset($_server[' Http_client_ip '])) {                $realip=$_server[' Http_client_ip ']; }Else{                $realip=$_server[' REMOTE_ADDR ']; }        }Else{            //do not allow to use getenv to obtain            if(getenv("Http_x_forwarded_for")){                  $realip=getenv("Http_x_forwarded_for"); }ElseIf(getenv("Http_client_ip")) {                  $realip=getenv("Http_client_ip"); }Else{                  $realip=getenv("REMOTE_ADDR"); }        }        return $realip; }          functionGetIP () {$ip=get_ip (); if($ip= = ' 127.0.0.1 '){            $myIp= ' Myip '; }         //Initialize        $curl=Curl_init (); //set the crawled URLcurl_setopt ($curl, Curlopt_url, ' http://ip.taobao.com/service/getIpInfo.php?ip= '.$myIp); //set the header file information as the data stream outputcurl_setopt ($curl, Curlopt_header, 0); //The information obtained is returned in the form of a file stream, rather than as a direct output. curl_setopt ($curl, Curlopt_returntransfer, 1); //Execute CommandCurl_multi_getcontent ($curl ); $data= Curl_exec ($curl); //Close URL RequestCurl_close ($curl); //show the data obtained        returnJson_decode ($data,true); }        functionWeather$chengshi){        $url= ' http://wthrcdn.etouch.cn/weather_mini?city= '.UrlEncode($chengshi); $html=file_get_contents($url); $jsondata= Gzdecode ($html); $data=json_decode ($jsondata,true); $arr=Array(); $arr[' Chengshi ']=$data[' Data '] [' City ']; $dangtian=$data[' Data '] [' Forecast '] [0]; $arr[' Gaowen ']=Str_replace("High Temperature",NULL,$dangtian[' High ']); $arr[' Diwen ']=Str_replace("Low Temperature",NULL,$dangtian[' Low ']); $arr[' Tianqi ']=$dangtian[' Type ']; return $arr; }        $area=GetIP (); if(isset($area[' Data '] [' City '])){        $city=$area[' Data '] [' City ']; $weather= Weather ($city); Print_r($weather); }

PHP interface get public IP and get weather interface information

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.