php ajax天气预报,AJAX_PHP+AJAX无刷新实现返回天气预报数据,用php来写一个天气预报的模块 - phpStudy...

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

header("Content-Type:text/html;charset=utf-8");

?>

weather forecast

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;

}

}

}

--请选择城市--

北京

上海

天津

重庆

广州

weatherforecast.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";

?>相关阅读:

JSP编译指令page、include详解

Node调试工具JSHint的安装及配置教程

基于C#实现的木马程序实例详解

详解IOS图层转场动画

Java中使用Socket发送Java对象实例

PHP图片处理之图片背景、画布操作

Android GestureDetector手势滑动使用实例讲解

js 获取input点选按钮的值的方法

Android从系统Gallery获取图片具体实现

判断Android程序是否在前台运行的两种方法

PHP基于yii框架实现生成ICO图标

ThinkPHP让分页保持搜索状态的方法

Android编程实现ListView头部ViewPager广告轮询图效果

Java Web项目中Spring框架处理JSON格式数据的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值