Home  >  Article  >  Web Front-end  >  Get weather API

Get weather API

一个新手
一个新手Original
2017-09-14 10:22:512053browse

1. Use the following code on the mobile phone

Step 1: Use Baidu’s api to get the current city

        $getIp=$_SERVER["REMOTE_ADDR"];

        $content =file_get_contents("http://api.map.baidu.com/location/ip?ak=6tNCG8mTxXruj6Cmek7heNS5099eDw0F&ip={$getIp}&coor=bd09ll");

        $json = json_decode($content);

        $address['city']=$json->{'content'}->{'address_detail'}->{'city'};

Step 2: Use the following code to obtain relevant weather information

        $data =file_get_contents('http://www.sojson.com/open/api/weather/json.shtml?city=哈尔滨市'); 

        $obj=json_decode($data);

        $city= $obj->city;//北京 1

        $a=$obj->data->forecast;

        $quality=$obj->data->quality; //空气质量 4

        $a1=trim(mb_substr($a[1]->high,2,-1,'utf-8'));//最高气温  3

        $b1=trim(mb_substr($a[1]->low,2,-1,'utf-8'));//最低气温

        $wendu=$a1."℃"."~".$b1."℃";

        $type=$a[1]->type;//天气 2

        dump($obj);

        die;

The above is the detailed content of Get weather API. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn