search
Homephp教程php手册php采集自中央气象台范围覆盖全国的天气预报代码实例

php采集自中央气象台范围覆盖全国的天气预报代码实例

Jun 13, 2016 am 09:08 AM
phpcodeweather forecastExampleMeteorological observatoryofscopecovercollection

php采集自中央气象台范围覆盖全国的天气预报代码实例

 这篇文章主要介绍了php采集自中央气象台范围覆盖全国的天气预报代码实例,较为详细的分析了采集的技巧及对应接口的调用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

 

 

本文实例讲述了php采集自中央气象台范围覆盖全国的天气预报代码。分享给大家供大家参考。具体分析如下:

天气预报信息采集自中央气象台,信息准确,覆盖面广,代码简单,返回是json代码,可以用于客户端调用,也可以在服务器端处理后显示,笔者的wordpress天气预报插件就是使用的这段代码,需要注意的是,这段代码会在服务器产生缓存文件,需要在当前目录中新建data文件夹,保证文件夹可写.

php天气预报代码如下:

代码如下:

/*
作者: freemouse
主页: www.cnphp.info
接口demo: http://www.cnphp.info/tianqi
用法: 可以直接调用上面的地址会显示当地的天气信息,也可以这样用
http://www.cnphp.info/tianqi/?q=江苏省南京市
**/

 

$p = $_get['q'];
$k = 0;
$encoding = mb_detect_encoding($p);
if($encoding != "utf-8"){
$p = mb_convert_encoding($p,"utf-8","gbk");
}
$p_arr = array(
"01" => "北京",
"02" => "上海",
"03" => "天津",
"04" => "重庆",
"05" => "黑龙江",
"06" => "吉林",
"07" => "辽宁",
"08" => "内蒙古",
"09" => "河北",
"10" => "山西",
"11" => "陕西",
"12" => "山东",
"13" => "新疆",
"14" => "西藏",
"15" => "青海",
"16" => "甘肃",
"17" => "宁夏",
"18" => "河南",
"19" => "江苏",
"20" => "湖北",
"21" => "浙江",
"22" => "安徽",
"23" => "福建",
"24" => "江西",
"25" => "湖南",
"26" => "贵州",
"27" => "四川",
"28" => "广东",
"29" => "云南",
"30" => "广西",
"31" => "海南",
"32" => "香港",
"33" => "澳门",
"34" => "台湾"
);

function find(&$item,$key,$data){
global $k;
if(preg_match("/$item/u",$data)){
$k = $key;
}
}

function get_data_arr($key){
if(!file_exists("./data/city{$key}.xml")){
$c =file_get_contents( "http://m.weather.com.cn/data5/city{$key}.xml" );
file_put_contents("./data/city{$key}.xml",$c);
}
else{
$c = file_get_contents("./data/city{$key}.xml");
}
$arr = explode(",",$c);
foreach($arr as $v){
$data = explode("|",$v);
$ret[$data[0]] = $data[1];
}
return $ret;
}

array_walk($p_arr,'find',$p);
array_walk(get_data_arr($k),'find',$p);
array_walk(get_data_arr($k),'find',$p);
$ccode = get_data_arr($k);
echo file_get_contents("http://m.weather.com.cn/data/{$ccode[$k]}.html");

 

希望本文所述对大家的php程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor