`
shenjun134
  • 浏览: 16986 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

通过城市代码获取免费天气预报

    博客分类:
  • PHP
 
阅读更多
这里主要用到就是通过城市名称获取城市代码,拼装URL去方位免费天气预报的网站获取资源信息,然后保存解析,展现给user。
如果想要运行下面的代码,首先要新建一个PHP的project,把下面三个文件拷贝进去,然后开启server,在浏览器上运行,如果不能访问,可以转换下你PC的代理,应该可以访问。有问题可以联系我~谢谢


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="weatherInfo.php" method="post">
CITY: <input type="text" name="city"/>
<input type="submit"/>

</form>
</body>
</html>


<?php


/*
 * Created on Aug 1, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
ini_set('default_charset','utf-8');
echo 'weather information<br/>';

$city = $_POST["city"];

$weatherInfo = getInfo($city);
save($weatherInfo);
display($weatherInfo);

function getInfo($city) {
	$url = "http://m.weather.com.cn/data/" . getCityCode($city) . ".html";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_PROXY, "xxx.xxx.xxx:80");
	curl_setopt($ch, CURLOPT_PROXYPORT, 80);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

function save($content){
	$fileName = "./info.txt";
	$file = fopen($fileName, "w");
	fwrite($file, $content);
	fclose($file);
}

function getCityCode($city) {
	$content = file_get_contents("./city.txt");
	$cities = json_decode($content);
	return $cities-> {$city};
}

function display($content) {
	
	$info = json_decode($content);
	$weatherInfo =  $info->{"weatherinfo"};
	showToday($weatherInfo);
	echo "<br/>";
	showTomorrow($weatherInfo);
}

function showToday($weatherInfo){
	echo "城市:" . $weatherInfo->{"city"} . "<br/>";
	echo "日期:" . $weatherInfo->{"date_y"} . "  " . $weatherInfo->{"week"} . "<br/>";
	echo "温度:" . $weatherInfo->{"temp1"} . "<br/>" ;
	echo "天气:" . $weatherInfo->{"weather1"} . "  " . $weatherInfo->{"wind1"} . "<br/>";
	echo $weatherInfo->{"index_d"} . "<br/>";
}

function showTomorrow($weatherInfo){
	echo "城市:" . $weatherInfo->{"city"} . "<br/>";
	echo "日期:" . getDateByCount(1) . "  " . getDayByCount(1) . "<br/>";
	echo "温度:" . $weatherInfo->{"temp2"} . "<br/>" ;
	echo "天气:" . $weatherInfo->{"weather2"} . "  " . $weatherInfo->{"wind2"} . "<br/>";
	echo $weatherInfo->{"index48_d"} . "<br/>";
}

function getDateByCount($count){
	$time = time() + 60 * 60 * 24 *  $count;
	return date("Y年n月d日", $time);
}

function getDayByCount($count){
	$time = time() + 60 * 60 * 24 *  $count;
	return getWeek(date("N", $time));
}

function getWeek($num){
	switch ($num){
		case "0":
			return "星期天";
			break;
		case "1":
			return "星期一";
			break;
		case "2":
			return "星期二";
			break;
		case "3":
			return "星期三";
			break;
		case "4":
			return "星期四";
			break;
		case "5":
			return "星期五";
			break;
		case "6":
			return "星期六";
			break;
		default:
			return "数字:" . $num . " 没有对应的week!";
			break;					
	}
	
}


city.txt就是存放城市代码的

{"北京":
"101010100"
,

"朝阳":
"101010300"
,

"顺义":
"101010400"
,

"怀柔":
"101010500"
,

"通州":
"101010600"
,

"昌平":
"101010700"
,

"延庆":
"101010800"
,

"丰台":
"101010900"
,

"石景山":
"101011000"
,

"大兴":
"101011100"
,

"房山":
"101011200"
,

"密云":
"101011300"
,

"门头沟":
"101011400"
,

"平谷":
"101011500"
,

"八达岭":
"101011600"
,

"佛爷顶":
"101011700"
,

"汤河口":
"101011800"
,

"密云上甸子":
"101011900"
,

"斋堂":
"101012000"
,

"霞云岭":
"101012100"
,

"北京城区":
"101012200"
,

"海淀":
"101010200"
				,

"天津":
"101030100"
,

"宝坻":
"101030300"
,

"东丽":
"101030400"
,

"西青":
"101030500"
,

"北辰":
"101030600"
,

"蓟县":
"101031400"
,

"汉沽":
"101030800"
,

"静海":
"101030900"
,

"津南":
"101031000"
,

"塘沽":
"101031100"
,

"大港":
"101031200"
,

"武清":
"101030200"
,

"宁河":
"101030700"
				,
"上海":
"101020100"
,

"宝山":
"101020300"
,

"嘉定":
"101020500"
,

"南汇":
"101020600"
,

"浦东":
"101021300"
,

"青浦":
"101020800"
,

"松江":
"101020900"
,

"奉贤":
"101021000"
,

"崇明":
"101021100"
,

"徐家汇":
"101021200"
,

"闵行":
"101020200"
,

"金山":
"101020700"
				,
"石家庄":
"101090101"
,

"张家口":
"101090301"
,

"承德":
"101090402"
,

"唐山":
"101090501"
,

"秦皇岛":
"101091101"
,

"沧州":
"101090701"
,

"衡水":
"101090801"
,

"邢台":
"101090901"
,

"邯郸":
"101091001"
,

"保定":
"101090201"
,

"廊坊":
"101090601"
				,
					"郑州":
"101180101"
,

"新乡":
"101180301"
,

"许昌":
"101180401"
,

"平顶山":
"101180501"
,

"信阳":
"101180601"
,

"南阳":
"101180701"
,

"开封":
"101180801"
,

"洛阳":
"101180901"
,

"商丘":
"101181001"
,

"焦作":
"101181101"
,

"鹤壁":
"101181201"
,

"濮阳":
"101181301"
,

"周口":
"101181401"
,

"漯河":
"101181501"
,

"驻马店":
"101181601"
,

"三门峡":
"101181701"
,

"济源":
"101181801"
,

"安阳":
"101180201"
				,
"合肥":
"101220101"
,

"芜湖":
"101220301"
,

"淮南":
"101220401"
,

"马鞍山":
"101220501"
,

"安庆":
"101220601"
,

"宿州":
"101220701"
,

"阜阳":
"101220801"
,

"亳州":
"101220901"
,

"黄山":
"101221001"
,

"滁州":
"101221101"
,

"淮北":
"101221201"
,

"铜陵":
"101221301"
,

"宣城":
"101221401"
,

"六安":
"101221501"
,

"巢湖":
"101221601"
,

"池州":
"101221701"
,

"蚌埠":
"101220201"
,
"杭州":
"101210101"
,

"舟山":
"101211101"
,

"湖州":
"101210201"
,

"嘉兴":
"101210301"
,

"金华":
"101210901"
,

"绍兴":
"101210501"
,

"台州":
"101210601"
,

"温州":
"101210701"
,

"丽水":
"101210801"
,

"衢州":
"101211001"
,

"宁波":
"101210401"
,
"重庆":
"101040100"
,

"合川":
"101040300"
,

"南川":
"101040400"
,

"江津":
"101040500"
,

"万盛":
"101040600"
,

"渝北":
"101040700"
,

"北碚":
"101040800"
,

"巴南":
"101040900"
,

"长寿":
"101041000"
,

"黔江":
"101041100"
,

"万州天城":
"101041200"
,

"万州龙宝":
"101041300"
,

"涪陵":
"101041400"
,

"开县":
"101041500"
,

"城口":
"101041600"
,

"云阳":
"101041700"
,

"巫溪":
"101041800"
,

"奉节":
"101041900"
,

"巫山":
"101042000"
,

"潼南":
"101042100"
,

"垫江":
"101042200"
,

"梁平":
"101042300"
,

"忠县":
"101042400"
,

"石柱":
"101042500"
,

"大足":
"101042600"
,

"荣昌":
"101042700"
,

"铜梁":
"101042800"
,

"璧山":
"101042900"
,

"丰都":
"101043000"
,

"武隆":
"101043100"
,

"彭水":
"101043200"
,

"綦江":
"101043300"
,

"酉阳":
"101043400"
,

"秀山":
"101043600"
,

"沙坪坝":
"101043700"
,

"永川":
"101040200"
,
"福州":
"101230101"
,

"泉州":
"101230501"
,

"漳州":
"101230601"
,

"龙岩":
"101230701"
,

"晋江":
"101230509"
,

"南平":
"101230901"
,

"厦门":
"101230201"
,

"宁德":
"101230301"
,

"莆田":
"101230401"
,

"三明":
"101230801"
,
"兰州":
"101160101"
,

"平凉":
"101160301"
,

"庆阳":
"101160401"
,

"武威":
"101160501"
,

"金昌":
"101160601"
,

"嘉峪关":
"101161401"
,

"酒泉":
"101160801"
,

"天水":
"101160901"
,

"武都":
"101161001"
,

"临夏":
"101161101"
,

"合作":
"101161201"
,

"白银":
"101161301"
,

"定西":
"101160201"
,

"张掖":
"101160701"
,
"广州":
"101280101"
,

"惠州":
"101280301"
,

"梅州":
"101280401"
,

"汕头":
"101280501"
,

"深圳":
"101280601"
,

"珠海":
"101280701"
,

"佛山":
"101280800"
,

"肇庆":
"101280901"
,

"湛江":
"101281001"
,

"江门":
"101281101"
,

"河源":
"101281201"
,

"清远":
"101281301"
,

"云浮":
"101281401"
,

"潮州":
"101281501"
,

"东莞":
"101281601"
,

"中山":
"101281701"
,

"阳江":
"101281801"
,

"揭阳":
"101281901"
,

"茂名":
"101282001"
,

"汕尾":
"101282101"
,

"韶关":
"101280201"
,
"南宁":
"101300101"
,

"柳州":
"101300301"
,

"来宾":
"101300401"
,

"桂林":
"101300501"
,

"梧州":
"101300601"
,

"防城港":
"101301401"
,

"贵港":
"101300801"
,

"玉林":
"101300901"
,

"百色":
"101301001"
,

"钦州":
"101301101"
,

"河池":
"101301201"
,

"北海":
"101301301"
,

"崇左":
"101300201"
,

"贺州":
"101300701"
,
"贵阳":
"101260101"
,

"安顺":
"101260301"
,

"都匀":
"101260401"
,

"兴义":
"101260906"
,

"铜仁":
"101260601"
,

"毕节":
"101260701"
,

"六盘水":
"101260801"
,

"遵义":
"101260201"
,

"凯里":
"101260501"
,
"昆明":
"101290101"
,

"红河":
"101290301"
,

"文山":
"101290601"
,

"玉溪":
"101290701"
,

"楚雄":
"101290801"
,

"普洱":
"101290901"
,

"昭通":
"101291001"
,

"临沧":
"101291101"
,

"怒江":
"101291201"
,

"香格里拉":
"101291301"
,

"丽江":
"101291401"
,

"德宏":
"101291501"
,

"景洪":
"101291601"
,

"大理":
"101290201"
,

"曲靖":
"101290401"
,

"保山":
"101290501"
,
"呼和浩特":
"101080101"
,

"乌海":
"101080301"
,

"集宁":
"101080401"
,

"通辽":
"101080501"
,

"阿拉善左旗":
"101081201"
,

"鄂尔多斯":
"101080701"
,

"临河":
"101080801"
,

"锡林浩特":
"101080901"
,

"呼伦贝尔":
"101081000"
,

"乌兰浩特":
"101081101"
,

"包头":
"101080201"
,

"赤峰":
"101080601"
,
"南昌":
"101240101"
,

"上饶":
"101240301"
,

"抚州":
"101240401"
,

"宜春":
"101240501"
,

"鹰潭":
"101241101"
,

"赣州":
"101240701"
,

"景德镇":
"101240801"
,

"萍乡":
"101240901"
,

"新余":
"101241001"
,

"九江":
"101240201"
,

"吉安":
"101240601"
,
"武汉":
"101200101"
,

"黄冈":
"101200501"
,

"荆州":
"101200801"
,

"宜昌":
"101200901"
,

"恩施":
"101201001"
,

"十堰":
"101201101"
,

"神农架":
"101201201"
,

"随州":
"101201301"
,

"荆门":
"101201401"
,

"天门":
"101201501"
,

"仙桃":
"101201601"
,

"潜江":
"101201701"
,

"襄樊":
"101200201"
,

"鄂州":
"101200301"
,

"孝感":
"101200401"
,

"黄石":
"101200601"
,

"咸宁":
"101200701"
,
"成都":
"101270101"
,

"自贡":
"101270301"
,

"绵阳":
"101270401"
,

"南充":
"101270501"
,

"达州":
"101270601"
,

"遂宁":
"101270701"
,

"广安":
"101270801"
,

"巴中":
"101270901"
,

"泸州":
"101271001"
,

"宜宾":
"101271101"
,

"内江":
"101271201"
,

"资阳":
"101271301"
,

"乐山":
"101271401"
,

"眉山":
"101271501"
,

"凉山":
"101271601"
,

"雅安":
"101271701"
,

"甘孜":
"101271801"
,

"阿坝":
"101271901"
,

"德阳":
"101272001"
,

"广元":
"101272101"
,

"攀枝花":
"101270201"
,
"银川":
"101170101"
,

"中卫":
"101170501"
,

"固原":
"101170401"
,

"石嘴山":
"101170201"
,

"吴忠":
"101170301"
,
"西宁":
"101150101"
,

"黄南":
"101150301"
,

"海北":
"101150801"
,

"果洛":
"101150501"
,

"玉树":
"101150601"
,

"海西":
"101150701"
,

"海东":
"101150201"
,

"海南":
"101150401"
,
"济南":
"101120101"
,

"潍坊":
"101120601"
,

"临沂":
"101120901"
,

"菏泽":
"101121001"
,

"滨州":
"101121101"
,

"东营":
"101121201"
,

"威海":
"101121301"
,

"枣庄":
"101121401"
,

"日照":
"101121501"
,

"莱芜":
"101121601"
,

"聊城":
"101121701"
,

"青岛":
"101120201"
,

"淄博":
"101120301"
,

"德州":
"101120401"
,

"烟台":
"101120501"
,

"济宁":
"101120701"
,

"泰安":
"101120801"
,
"西安":
"101110101"
,

"延安":
"101110300"
,

"榆林":
"101110401"
,

"铜川":
"101111001"
,

"商洛":
"101110601"
,

"安康":
"101110701"
,

"汉中":
"101110801"
,

"宝鸡":
"101110901"
,

"咸阳":
"101110200"
,

"渭南":
"101110501"
,
"太原":
"101100101"
,

"临汾":
"101100701"
,

"运城":
"101100801"
,

"朔州":
"101100901"
,

"忻州":
"101101001"
,

"长治":
"101100501"
,

"大同":
"101100201"
,

"阳泉":
"101100301"
,

"晋中":
"101100401"
,

"晋城":
"101100601"
,

"吕梁":
"101101100"
,
"乌鲁木齐":
"101130101"
,

"石河子":
"101130301"
,

"昌吉":
"101130401"
,

"吐鲁番":
"101130501"
,

"库尔勒":
"101130601"
,

"阿拉尔":
"101130701"
,

"阿克苏":
"101130801"
,

"喀什":
"101130901"
,

"伊宁":
"101131001"
,

"塔城":
"101131101"
,

"哈密":
"101131201"
,

"和田":
"101131301"
,

"阿勒泰":
"101131401"
,

"阿图什":
"101131501"
,

"博乐":
"101131601"
,

"克拉玛依":
"101130201"
,
"拉萨":
"101140101"
,

"山南":
"101140301"
,

"阿里":
"101140701"
,

"昌都":
"101140501"
,

"那曲":
"101140601"
,

"日喀则":
"101140201"
,

"林芝":
"101140401"
,
"台北县":
"101340101"
,

"高雄":
"101340201"
,

"台中":
"101340401"
,
"海口":
"101310101"
,

"三亚":
"101310201"
,

"东方":
"101310202"
,

"临高":
"101310203"
,

"澄迈":
"101310204"
,

"儋州":
"101310205"
,

"昌江":
"101310206"
,

"白沙":
"101310207"
,

"琼中":
"101310208"
,

"定安":
"101310209"
,

"屯昌":
"101310210"
,

"琼海":
"101310211"
,

"文昌":
"101310212"
,

"保亭":
"101310214"
,

"万宁":
"101310215"
,

"陵水":
"101310216"
,

"西沙":
"101310217"
,

"南沙岛":
"101310220"
,

"乐东":
"101310221"
,

"五指山":
"101310222"
,

"琼山":
"101310102"
,
"长沙":
"101250101"
,

"株洲":
"101250301"
,

"衡阳":
"101250401"
,

"郴州":
"101250501"
,

"常德":
"101250601"
,

"益阳":
"101250700"
,

"娄底":
"101250801"
,

"邵阳":
"101250901"
,

"岳阳":
"101251001"
,

"张家界":
"101251101"
,

"怀化":
"101251201"
,

"黔阳":
"101251301"
,

"永州":
"101251401"
,

"吉首":
"101251501"
,

"湘潭":
"101250201"
,
"南京":
"101190101"
,

"镇江":
"101190301"
,

"苏州":
"101190401"
,

"南通":
"101190501"
,

"扬州":
"101190601"
,

"宿迁":
"101191301"
,

"徐州":
"101190801"
,

"淮安":
"101190901"
,

"连云港":
"101191001"
,

"常州":
"101191101"
,

"泰州":
"101191201"
,

"无锡":
"101190201"
,

"盐城":
"101190701"
,
"哈尔滨":
"101050101"
,

"牡丹江":
"101050301"
,

"佳木斯":
"101050401"
,

"绥化":
"101050501"
,

"黑河":
"101050601"
,

"双鸭山":
"101051301"
,

"伊春":
"101050801"
,

"大庆":
"101050901"
,

"七台河":
"101051002"
,

"鸡西":
"101051101"
,

"鹤岗":
"101051201"
,

"齐齐哈尔":
"101050201"
,

"大兴安岭":
"101050701"
,
"长春":
"101060101"
,

"延吉":
"101060301"
,

"四平":
"101060401"
,

"白山":
"101060901"
,

"白城":
"101060601"
,

"辽源":
"101060701"
,

"松原":
"101060801"
,

"吉林":
"101060201"
,

"通化":
"101060501"
,
"沈阳":
"101070101"
,

"鞍山":
"101070301"
,

"抚顺":
"101070401"
,

"本溪":
"101070501"
,

"丹东":
"101070601"
,

"葫芦岛":
"101071401"
,

"营口":
"101070801"
,

"阜新":
"101070901"
,

"辽阳":
"101071001"
,

"铁岭":
"101071101"
,

"朝阳":
"101071201"
,

"盘锦":
"101071301"
,

"大连":
"101070201"
,

"锦州":
"101070701"
}
分享到:
评论

相关推荐

    android demo,通过城市名称获取到天气预报

    这个Android Demo是关于如何利用城市名称来获取天气预报信息的应用示例。在开发Android应用程序时,获取天气数据通常涉及到网络请求、数据解析以及用户界面展示等多个环节。下面将详细讲解这个Demo涉及的关键知识点...

    VB天气预报及QQ城市代码获取

    VB关于编写天气预报及QQ城市代码获取的源代码

    QQ天气预报城市代码获取

    QQ天气预报城市代码获取是开发一个集成天气预报功能时可能会遇到的一个步骤,特别是在自定义浏览器或者桌面应用中。为了能够准确地展示用户所在城市的天气信息,开发者需要知道该城市的特定代码,这通常是由提供天气...

    C#获取实时天气预报

    在本例中,我们将关注的是获取天气预报的SOAP Web服务。 要调用Web服务,我们需要在C#项目中添加对服务的引用。这可以通过Visual Studio的“添加服务引用”功能实现。输入提供天气预报的Web服务URL,例如...

    天气预报城市代码

    ### 天气预报城市代码知识点解析 #### 一、引言 随着信息技术的发展与普及,天气预报服务在人们日常生活中扮演着越来越重要的角色。对于开发者而言,为了提供准确且便捷的天气信息,需要掌握一系列与天气预报API...

    中国天气预报网城市对应代码

    了解这些城市代码对于从事气象数据分析、天气预报服务开发以及相关研究工作的人来说非常有用。例如,可以通过这些代码快速查询到特定城市的实时天气情况,为出行规划提供参考;或者利用这些数据进行气象模型的构建和...

    根据IP地址变化的天气预报代码 模仿腾讯天气预报效果

    根据IP地址变化的天气预报代码 模仿腾讯天气预报效果根据IP地址变化的天气预报代码 模仿腾讯天气预报效果

    易语言天气预报例程源码,易语言国内主要城市天气预报

    "取所有城市代码"这个功能,意味着程序会从某个天气预报服务提供商获取中国所有城市的代码,这些代码通常用于标识不同的城市,在请求天气数据时使用。城市代码可能是一个特定的数字或字符串,开发者需要正确地解析和...

    通过webservices获取当前天气预报

    这个工具与获取天气预报的Web服务有潜在的关联,因为它可能用到类似的天气数据来分析和对比本地环境与官方预报的差异,或者根据当前天气调整其功能。 总的来说,通过WebServices获取当前天气预报是一项常见的开发...

    天气预报代码各种样式

    在"天气预报"的场景下,JSP可以用来处理后台数据,如从API获取当前及未来几天的天气信息,进行计算和处理,然后将这些数据传递给前端展示。 HTML(HyperText Markup Language)是构建网页的基础,用于定义网页的...

    天气预报-城市码(plist)

    综上所述,`CityCode.plist`文件在iOS应用中用于天气预报功能,它包含了不同城市的代码,使得开发者能够通过查询这个文件来获取特定城市的天气信息。在实际开发中,正确地使用和管理这类资源文件对于实现功能和优化...

    安卓天气预报项目源代码

    【安卓天气预报项目源代码】是一个面向Android平台的软件开发项目,主要目标是实现一个能够实时获取并显示天气信息的应用程序。这个项目涉及到的技术点涵盖了Android应用开发的基础和进阶内容,包括用户界面设计、...

    html javascript 天气预报 (内附源代码)

    - `weatherWidget`:这个文件可能是整个天气预报组件的JavaScript源代码,包含了获取数据、解析数据、更新DOM和添加动画等功能的实现。通过阅读和理解这个文件,可以深入学习如何将HTML、CSS和JavaScript有效地结合...

    Android Studio实现天气预报APP系统源码(也可做Android本科毕业设计).zip

    介绍做的一个天气预报demo,界面美观,实现了自动获取当前位置天气信息、根据天气更换背景图、更换城市、热门城市列表、记住设置的地址等功能。软件架构使用了android开发。城市管理界面:在这里可以对添加的城市...

    天气预报的城市代码

    标题中的“天气预报的城市代码”指的是一个数据资源,它包含了用于识别各个城市的特定代码,这些代码通常被用于天气预报服务中,以便系统能够准确地获取和显示不同地区的天气信息。XML文件是原始的数据源,它可能...

    js 获取天气预报

    JavaScript(简称JS)是一种轻量级的脚本语言,常用于网页和网络应用开发...以上就是使用JS获取天气预报的基本流程和关键知识点,通过实践和不断学习,你可以构建出更复杂的天气应用,包括实时更新、多城市切换等功能。

    Android Studio实现天气预报App,满分课设,入门必学

    通过调用天气预报接口来获得天气数据,用LIstView和GridView来搭建每个界面,将查询的天气信息存储在SQLiteDatabase中。应用主要包括了五大界面,每一个界面上都会有很多组件: - 首页面:有各种组件装饰,显示天气...

    Android代码-城市天气预报源码.zip

    《Android城市天气预报源码解析》 Android城市天气预报源码是开发一款能够实时查询各地天气的应用程序的基础。这份源码包含了一系列与Android应用程序开发相关的技术知识点,涉及到UI设计、网络请求、数据处理等多...

Global site tag (gtag.js) - Google Analytics