• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    PHP+AJAX无刷新实现返回天气预报数据
    用php来写一个天气预报的模块

    天气数据是通过采集中国气象网站的。本来中国天气网站也给出了数据的API接口。以下是API的地址。返回的数据格式为json格式。

    1. http://www.weather.com.cn/data/sk/101010100.html
    2. http://www.weather.com.cn/data/cityinfo/101010100.html
    3. http://m.weather.com.cn/data/101010100.html

    URL中的数字”101010100“是城市代码。所以可以先列出每个城市的城市代码,然后php程序接收到了城市代码,再去组装URL,在通过URL来显示该城市的实时天气。

    index.php
    复制代码 代码如下:

    ?php
    header("Content-Type:text/html;charset=utf-8");
    ?>
    html>
    meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    head>
    title>weather forecast/title>
    script type="text/javascript" src="ajax.js">/script>
    script type="text/javascript">
    function $(id){
    return document.getElementById(id);
    }
    function getCityId(){
    var http_request=createAjax();
    var url="weatherforecast.php"
    var data="cityid="+$("cityId").value;
    http_request.onreadystatechange=getWetherInfo;
    http_request.open("post",url,true);
    http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    http_request.send(data);
    function getWetherInfo(){
    if(http_request.readyState==4 http_request.status==200){
    var info=http_request.responseText;
    $("weatherinfo").innerHTML=info;
    }
    }
    }
    /script>
    /head>
    body>
    select name="cityId" onchange="getCityId();" id="cityId">
    option>--请选择城市--/option>
    option value="101010100">北京/option>
    option value="101020100">上海/option>
    option value="101030100">天津/option>
    option value="101040100">重庆/option>
    option value="101280101">广州/option>
    /select>
    span id="weatherinfo">/span>
    /body>
    /html>

    weatherforecast.php
    复制代码 代码如下:

    ?php
    header("Content-Type:text/html;charset=utf-8");
    header("Cache-Control:no-cache");
    if (isset($_POST['cityid'])){
    $cityid=$_POST['cityid'];
    $url=$url="http://www.weather.com.cn/data/sk/".$cityid.".html";
    }else {
    $url="http://www.weather.com.cn/data/sk/101010100.html";
    }
    $weatherInfo_json=file_get_contents($url);
    $weatherInfo=json_decode($weatherInfo_json,true);
    $cityName=$weatherInfo['weatherinfo']['city'];
    $cityTemp=$weatherInfo['weatherinfo']['temp'];
    $cityWd=$weatherInfo['weatherinfo']['WD'];
    $cityWs=$weatherInfo['weatherinfo']['WS'];
    $cityTime=$weatherInfo['weatherinfo']['time'];
    $citySD=$weatherInfo['weatherinfo']['SD'];
    echo $weatherinfo="城市名字:$cityName,气温:$cityTemp,风向:$cityWd";
    ?>
    您可能感兴趣的文章:
    • PHP调用API接口实现天气查询功能的示例
    • PHP微信开发之查询城市天气
    • php使用百度天气接口示例
    • PHP实现采集中国天气网未来7天天气
    • php结合飞信 免费天气预报短信
    • PHP 获取指定地区的天气实例代码
    • php实现的百度搜索某地天气的小偷代码
    • PHP Ajax JavaScript Json获取天气信息实现代码
    • php采集自中央气象台范围覆盖全国的天气预报代码实例
    • php微信开发之百度天气预报
    • PHP调用全国天气预报数据接口查询天气示例
    上一篇:jquery ajax 向后台传递数组参数示例
    下一篇:jQuery的ajax传参巧用JSON使用示例(附Json插件)
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    PHP+AJAX无刷新实现返回天气预报数据 PHP+AJAX,无,刷新,实现,返回,