×

天气预报接口php语言

天气预报接口php语言(天气预报接口api)

admin admin 发表于2023-03-20 20:48:07 浏览33 评论0

抢沙发发表评论

本文目录一览:

php怎么解析天气预报api返回的数据

json就要用json的形式返回啊 就行ajax返回数据一样 它的返回值是true 就是正确的 如果是返回ajax的话,先判断data 为真就直接data.XX(字段名) 就可以取出来了

php获取天气预报的代码

?php    

$URLStyle = "";    

$chinaURL = sprintf($URLStyle, "china");    

$chinaStr = file_get_contents($chinaURL);    

$chinaObj = simplexml_load_string($chinaStr);    

$chinaObjLen = count($chinaObj-city);    

echo "chinaObjLen = ".$chinaObjLen."\n";    

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."\n";    

        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: not exsit next level node. - ".$level1."-".$shijiURL."\n";    -天气预报接口php语言

                        echo '  "'.$shengjiObj-city[$j]["cityname"].'" = ';    

                        echo $shengjiObj-city[$j]["url"].",\n";    

                        continue;    

                }    

                $shijiObjLen = count($shijiObj-city);    

                //echo $shengjiObj-city[$j]["cityname"]."  ";    

                //echo $shijiObjLen."\n";    

                for ($k=0;$k$shijiObjLen;$k++){    

                //遍历县一级节点    

                        $xianji_code = $shijiObj-city[$k]["url"];    

                        echo '  "'.$shijiObj-city[$k]["cityname"].'" = ';    

                        echo $shijiObj-city[$k]["url"].",\n";    

                        //echo $xianji_code."\n";     

                }    

        }    

}           

//print_r($chinaObj);    

?

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

百度首页的天气预报显示用php怎么实现

方式一、你可以去气象局的网站使用php(python)爬虫抓取网页HTML内容提取其中的信息即可。

方式二、气象局的网站一般提供了免费的API接口,可以得到一个封装好的JSON数据包,拆开就能得到很多信息

php怎么抓取天气预报?

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

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

?php

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

function getWeather($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('/table class=\"FcstBoxTable01\" [^]*[^]*(.*)\/div/si',$result, $matches, PREG_SET_ORDER);-天气预报接口php语言

preg_match_all('/td nowrap=\"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 = \"\";

这里是读取资料的网址

上面的是台湾中央气象局

preg_match_all('/table class=\"FcstBoxTable01\" [^]*[^]*(.*)\/div/si',$result, $matches, PREG_SET_ORDER);-天气预报接口php语言

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

这里是截取台湾中央气象局网页信息table class=\"FcstBoxTable01\" [^]*[^]*(.*)\/div的资料以及td nowrap=\"nowrap\" [^]*[^]*(.*)\/td的资料分别是1天跟1周-天气预报接口php语言

$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等等获取)