zoukankan      html  css  js  c++  java
  • php地方天气

    <?php
    header("Content-type: text/html; charset=utf-8");
    //获取IP的函数
    function GetIP(){
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
    $cip = $_SERVER["HTTP_CLIENT_IP"];
    }
    elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
    $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    elseif(!empty($_SERVER["REMOTE_ADDR"])){
    $cip = $_SERVER["REMOTE_ADDR"];
    }
    else{
    $cip = "无法获取!";
    }
    return $cip;
    }
    $ip= GetIP();
    $urllocation="http://api.map.baidu.com/location/ip?ak=TFyi03cqGpZAREZbhsboBTzE&ip=".$ip;
    $location=json_decode(send_post($urllocation),true);
    echo ($location["address"]);
    //baidu ipapi http://developer.baidu.com/map/index.php?title=webapi

    //发送post请求
    $url = 'http://apis.baidu.com/heweather/weather/free?city=wenzhou';
    function send_post($url){
    $ch = curl_init();
    $header = array(
    'apikey: 7728db103ad7e801f1225d488ddce370',
    );
    // 添加apikey到header
    curl_setopt($ch, CURLOPT_HTTPHEADER , $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 执行HTTP请求
    curl_setopt($ch , CURLOPT_URL , $url);
    return curl_exec($ch);
    }

    //天气数据存在$weather变量中
    $weahter=json_decode(send_post($url))->{"HeWeather data service 3.0"}[0];
    echo ($weahter->{"basic"}->{"city"}."<br>");
    echo ($weahter->{"now"}->{"cond"}->{"code"}."<br>");
    echo ($weahter->{"now"}->{"cond"}->{"txt"}."<br>");
    echo ($weahter->{"suggestion"}->{"comf"}->{"brf"}."<br>");
    echo ($weahter->{"suggestion"}->{"comf"}->{"txt"}."<br>");
    ?>

  • 相关阅读:
    Spring boot 启动图片
    Spring Cloud 从入门到入门
    理解错误的 Arrays.asList()
    git github 对代码的管理
    【POJ 2154】Color
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    [数据结构]Hash Table(哈希表)
  • 原文地址:https://www.cnblogs.com/wz0107/p/4993066.html
Copyright © 2011-2022 走看看