导航:首页 > 编程语言 > php显示天气预报

php显示天气预报

发布时间:2022-07-16 22:57:11

php在日历上面显示天气预报这么显示 我日历代码已经写好在下面这么添加天气预报

直接用中国天气网的插件就可以

Ⅱ 网页中加天气预报代码 能显示国外的天气

<iframe src=" http://tsov.net/tq/weather.php?bgcolor=383838&txtcolor=eee " width="205" height="131" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" border="0" /></iframe> 把这段代码中的 那个网址【 http://tsov.net/tq/weather.php?bgcolor=383838&txtcolor=eee 】换成有你 想要地方的天气的网址即可,另外注意高度和宽度要适合。原理很简单,你 可以往那儿放个网络试试 追问: <iframe src=" http://www.thinkpage.cn/weather/weather.aspx?c=UKXX0085&l=zh-TW&p=MSN&a=1&u=C&s=2&m=1&x=1&d=0&fc=00B0F0&bgc=&bc= " frameborder="0" scrolling="no" width="200" height="130" allowTransparency="true"></iframe> 这个是我头刚找到的 你说的我没试出来 谢谢 回答: 那我 都试出来了 呀,,刚才,你 可以吧那代码保存成HTML再打开看看是否如意 追问: <iframe src=" http://www..com/tq/weather.php?bgcolor=383838&txtcolor=eee " width="205" height="131" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" border="0" /></iframe> 是这样改吗 回答: <iframe src=" http://www.thinkpage.cn/weather/weather.aspx?c=UKXX0085&l=zh-TW&p=MSN&a=1&u=C&s=2&m=1&x=1&d=0&fc=00B0F0&bgc=&bc= " frameborder="0" scrolling="no" width="200" height="130" allowTransparency="true"></iframe> 就用这个代码,保存成HTML再打开看 追问: 这个不是我刚发给你的吗 这个我试过了 可以的 我是说你让我改的没试出来 回答: http://www..com/tq/weather.php?bgcolor=383838&txtcolor=eee " 这网址本来就打不开, 我的 意思是网址你可以随便换,只是注意宽和高,就行

记得采纳啊

Ⅲ php怎么抓取天气预报

可以借由php的api或者preg_match_all偷偷撷取去达成目的

这里给你一段我给台湾朋友有一段源码

<?php

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

$toURL="
$city.htm";
$post=array();
$ch=curl_init();
$options=array(
CURLOPT_REFERER=>'',
CURLOPT_URL=>$toURL,
CURLOPT_VERBOSE=>0,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_USERAGENT=>"Mozilla/4.0(compatible;)",
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>http_build_query($post),
);
curl_setopt_array($ch,$options);

$result=curl_exec($ch);
curl_close($ch);
//连接中央气象局
echo'<pre>';
preg_match_all('/<tableclass="FcstBoxTable01"[^>]*[^>]*>(.*)</div>/si',$result,$matches,PREG_SET_ORDER);

preg_match_all('/<tdnowrap="nowrap"[^>]*[^>]*>(.*)</td>/si',$matches[0][1],$m1,PREG_SET_ORDER);

$m2=explode('</td>',$m1[0][1]);
//print_r($m2);//取得每日资料m2[0~6]

$weather=array();
for($i=0;$i<=6;$i++){

preg_match_all('/src=[^>]*[^>](.*)/si',$m2[$i],$m5,PREG_SET_ORDER);//取得天气图档
$m6=explode('"',$m5[0][0]);
$wi='
($m6[1],'../../');
$wtitle=$m6[3];
print_r($wtitle);
$weather[$i]['date']=date("m-d",mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
$weather[$i]['temperature']=trim(strip_tags($m2[$i]));
$weather[$i]['title']=$wtitle;
$weather[$i]['img']=$wi;
}

return($weather);

}

$weather=getWeather("Taipei_City");
print_r($weather);


//header("Location:loc.php");
?>

首先

$toURL="http://www.cwb.gov.tw/V7/forecast/taiwan/inc/city/$city.htm";

这里是读取资料的网址

上面的是台湾中央气象局

	preg_match_all('/<tableclass="FcstBoxTable01"[^>]*[^>]*>(.*)</div>/si',$result,$matches,PREG_SET_ORDER);

preg_match_all('/<tdnowrap="nowrap"[^>]*[^>]*>(.*)</td>/si',$matches[0][1],$m1,PREG_SET_ORDER);

这里是截取台湾中央气象局网页信息table class="FcstBoxTable01" [^>]*[^>]*>(.*)</div>的资料以及<td nowrap="nowrap" [^>]*[^>]*>(.*)</td>的资料分别是1天跟1周

$m2=explode('</td>',$m1[0][1]);
//print_r($m2);//取得每日资料m2[0~6]

这里是取得每日的资料

preg_match_all('/src=[^>]*[^>](.*)/si',$m2[$i],$m5,PREG_SET_ORDER);//取得天气图档

这里是取得天气的图档

		$m6=explode('"',$m5[0][0]);
$wi='
($m6[1],'../../');
$wtitle=$m6[3];
print_r($wtitle);
$weather[$i]['date']=date("m-d",mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
$weather[$i]['temperature']=trim(strip_tags($m2[$i]));
$weather[$i]['title']=$wtitle;
$weather[$i]['img']=$wi;

这里是返回的网址,日期,标题,图档等等的资料

$weather=getWeather("Taipei_City");
print_r($weather);

然后这里是显示出地区的一周天气预报


结论:就是如果你想从网站上面截取天气预报

在php可以是用preg_match_all(网页的表格table,表格的列数tr,表格的栏位td,或者更加广泛的标签div等等获取)

Ⅳ php的socket调用可以实现查天气吗

本文分享下,php调用yahoo与sina的天气api,实现实时显示天气预报的代码,有兴趣的朋友研究下吧。

yahoo 天气预报
地址 http://developer.yahoo.com/weather/
代码:

复制代码代码示例:
<?php
header ( 'Content-Type: text/html; charset = utf-8' );
class weather {
static $url = 'http://xml.weather.yahoo.com/forecastrss?u=c&w=';
static $city = 'Beijing'; //默认城市北京 这里要注意的是 city 要填拼音 我试过用中文有好几个地区都调用不到
static $weatherXML = '';
static $woeid_file = "woeid";
static $file_path = "data/";

/**
* 获得远程xml并缓存到本地
*/
static public function getXML($city = null) {

if ($city != null){
self::$city = $city;
}
self::$weatherXML = self::$file_path . md5(self::$city) . '-weather.xml';
if (file_exists( self::$weatherXML )) {
$fileTime = filemtime ( self::$weatherXML );
$stater = time () - $fileTime - 60 * 60 * 2;
if ($stater > 0) {
return true;
}
}
//获取woeid
$woeid = self::getWOEID();
self::$url = self::$url . $woeid[0];
//获取当天 天气
$XML = self::vget(self::$url);
//保存当天 天气到文件
self::cacheXML($XML);

self::analysisXML($XML);
}

static public function analysisXML($simple) {

$p = xml_parser_create();

xml_parse_into_struct($p, $simple, $vals, $index);

xml_parser_free($p);

//本周天气
$weekindex = $index['YWEATHER:FORECAST'];
$week = array();
foreach($weekindex as $k=>$v){
$week[$k] = $vals[$v]['attributes'];
}
unset($index);
unset($vals);
print_r($week);
/*
<yweather:forecast day="Wed" date="18 Sep 2013" low="20" high="32" text="Sunny" code="32"/>
* day 星期
* date 日期
* low 最低温度
* high 最高温度
* test 天气状态
* code 天气图标
*/
}
/*
* 取得地区WOEID码
*/
static private function getWOEID(){
static $woeid = array();

if(isset($woeid[self::$city])){
return $woeid[self::$city];
}

if (file_exists( self::$file_path . self::$woeid_file )) {
$woeidSTR = file_get_contents(self::$file_path . self::$woeid_file);
$woeid = json_decode($woeidSTR , true);
if(isset($woeid[self::$city])){
return $woeid[self::$city];
}
}
$geoPlaces = "http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text='".self::$city."%20CH'";

$XML = simplexml_load_file( $geoPlaces );

if(isset($XML->results->place[0])){
$rs = $woeid[self::$city] = $XML->results->place[0]->woeid;
//保存到文件
$woeidSTR = json_encode($woeid);
file_put_contents(self::$file_path . self::$woeid_file, $woeidSTR);
return $rs;
}else{
//如果找不到城市 woeid 默认城市就改为 北京
self::$city = "Beijing";

return self::getWOEID();
}
}
/**
* 创建xml缓存
* @param $contents 要缓存的内容
*/
static private function cacheXML($contents) {
$contents = str_ireplace ( '<?xml version="1.0"?>', "<?xml version=\"1.0\"?> \n", $contents );
$contents = mb_convert_encoding ( $contents, 'utf-8', 'gbk' );
file_put_contents ( self::$weatherXML, $contents ) or die ( '没有写权限' );
}
/**
* 模拟获取内容函数
* @param type $url
* @return type
*/

static private function vget($url) {
$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
$curl = curl_init (); // 启动一个CURL会话
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 从证书中检查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $user_agent ); // 模拟用户使用的浏览器
@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 发送一个常规的Post请求
curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec ( $curl ); // 执行操作
if (curl_errno ( $curl )) {
curl_close ( $curl ); // 关闭CURL会话
die('Errno' . curl_error ( $curl )) ;
}
curl_close ( $curl ); // 关闭CURL会话
return $tmpInfo; // 返回数据
}
}
weather::getXML("Changsha");

Ⅳ PHP 页面调用天气预报web服务 我想在一个PHP页面直接调用现成的webservice

完全可以。前提是要打开soap扩展,调用方法如下:
<?php
$client = new SoapClient('http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl');
$parm=array('theCityCode'=>'三亚','theUserID'=>'');
$result=$client->getWeather($parm);
print_r($result);
?>

Ⅵ php获取天气预报的代码

<?php
$URLStyle="http://flash.weather.com.cn/wmaps/xml/%s.xml";
$chinaURL=sprintf($URLStyle,"china");
$chinaStr=file_get_contents($chinaURL);
$chinaObj=simplexml_load_string($chinaStr);
$chinaObjLen=count($chinaObj->city);
echo"chinaObjLen=".$chinaObjLen." ";
for($i=0;$i<$chinaObjLen;$i++){
//遍历省一级节点,共37个
$level1=$chinaObj->city[$i]["pyName"];
$shengjiURL=sprintf($URLStyle,$level1);
$shengjiStr=file_get_contents($shengjiURL);
//echo$shengjiStr;
$shengjiObj=simplexml_load_string($shengjiStr);
$shengjiObjLen=count($shengjiObj->city);
//echo$chinaObj->city[$i]["quName"];
//echo"".$shengjiObjLen." ";
for($j=0;$j<$shengjiObjLen;$j++){
//遍历市一级节点
$level2=$shengjiObj->city[$j]["pyName"];
$shijiURL=sprintf($URLStyle,$level2);
$shijiStr=file_get_contents($shijiURL);
//echo$shijiStr;
$shijiObj=simplexml_load_string($shijiStr);
//直辖市和海南、台湾、钓鱼岛等没有县级节点
if(!$shijiObj){
echo"WARNNING:notexsitnextlevelnode.-".$level1."-".$shijiURL." ";
echo'"'.$shengjiObj->city[$j]["cityname"].'"=>';
echo$shengjiObj->city[$j]["url"].", ";
continue;
}
$shijiObjLen=count($shijiObj->city);
//echo$shengjiObj->city[$j]["cityname"]."";
//echo$shijiObjLen." ";
for($k=0;$k<$shijiObjLen;$k++){
//遍历县一级节点
$xianji_code=$shijiObj->city[$k]["url"];
echo'"'.$shijiObj->city[$k]["cityname"].'"=>';
echo$shijiObj->city[$k]["url"].", ";
//echo$xianji_code." ";
}
}
}
//print_r($chinaObj);
?>

通过XML接口根节点递归获得全国几千个县以上城市cide code的代码

Ⅶ 怎么用php抓取天气预报先说下思路,再举个例子。

我昨天刚做了个,本来打算自己用,你既然问,就分享了吧!
PS:因为我是菏泽人,所以,如果你什么都不填,就显示菏泽天气。
如果想显示别的,直接输入就行,比如"香港",然后提交就OK了
http://young.boustead.e.cn/data/shunzi/tq.php
不知道是否合乎楼主的意愿(不用JS)

Ⅷ php如何获得当地的天气预报 - 技术问答

这个无需PHP做。前端就可以获取,直接搜索天气API,会有很多天气接口。
http://www.weather.com.cn/data/sk/101010100.html
或者
http://cj.weather.com.cn/

Ⅸ 天气预报代码

根据IP地址,显示当地的天气预报:
<iframe src="http://www.tianqi123.com/php/current_city.php" width=178 height=248 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no align=center id=url></iframe>

第二个:265的.
<iframe src=http://www.cms52.cn/cms/20080301/"http://weather.265.com/weather.htm" width="168" height="54" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no">
</iframe>

第三个:腾讯QQ的:
<iframe SRC='http://weather.qq.com/inc/ss258.htm' id='ifm2' WIDTH='189' HEIGHT='190' ALIGN='CENTER' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO'></iframe>

第四个:还是腾讯QQ的:
<iframe SRC=http://www.cms52.cn/cms/20080301/"http://weather.qq.com/24.htm" id='ifm1' WIDTH='405' HEIGHT='332' ALIGN='center' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO'></iframe>

再送你一款QQ电台的调用,蛮小巧的:
<IFRAME marginWidth=0 marginHeight=0 src=http://www.cms52.cn/cms/20080301/"http://listen.qq.com/cj/player.htm" frameBorder=0 width=255 scrolling=no height=30></IFRAME>

阅读全文

与php显示天气预报相关的资料

热点内容
oraclelinux安装目录 浏览:133
安卓系统可以安装编译器吗 浏览:570
javajson实体类 浏览:690
板加密钢筋是否取代原钢筋 浏览:66
学习编程的思路 浏览:230
app易语言post怎么学 浏览:965
地梁的箍筋加密区位置 浏览:302
二分法排序程序及编译结果 浏览:679
日语命令形和禁止型 浏览:285
安装软件用管理员解压 浏览:505
编译原理代码块 浏览:400
小孩可以用压缩面膜吗 浏览:14
锥形倒角怎么计算法 浏览:882
java合并链表 浏览:508
pic单片机编译器 浏览:806
丽水四轴加工中心编程 浏览:691
国产系统怎么解压 浏览:553
战双程序员 浏览:483
him触摸编程软件 浏览:932
植物大战僵尸存档怎么转移安卓 浏览:852