php读取百度天气api(php天气预报)

本文目录一览:

php网页的api使用 比如我自己的一个网页,想要使用天气网提供的api,如何得到数据,如何处理这

接口会返回json数据,用php提供的json_decode函数可以将其转为对象或者数组,再输出即可。 可以参考我的网站

如何使用PHP调用API接口实现天气查询功能

最近在做微信公众平台测试时,想在里面子菜单上添加查询未来几天(包括今天)天气的功能,就查找了下好用的天气预报查询接口API,使用比较多的有:国家气象局天气接口、新浪天气预报接口、百度天气预报接口、google天气接口、Yahoo天气接口等等,我使用的是百度提供的免费天气查询接口API,下面与大家分享下…

1、查询方式:

百度提供的是根据纬度和城市名查询天气情况

2、接口事例:

3、接口参数说明:

4、返回结果说明:

5、

//城市名

$city = ‘上海’;

//对json格式的字符串进行编码

$arr =json_decode($str,TRUE);

print_r($atr);

//城市名

   $city = ‘上海’;

 

   //获取json格式的数据

   $str =file_get_contents(“”.$city.”output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ”);

   //对json格式的字符串进行编码

   $arr =json_decode($str,TRUE);      

   print_r($atr);

6、返回页面的是json编码后的数据:

[plain] view plain copy print?

meta charset=”UTF-8″

Array

(

[error] = 0

[status] = success

[date] = 2014-03-17

[results] = Array

(

[0] = Array

(

[currentCity]= 上海

[weather_data]= Array

(

[0]= Array

(

[date] = 周一(今天, 实时:19℃)

[dayPictureUrl] =

[nightPictureUrl] =

[weather] = 晴

[wind] = 西南风3-4级

[temperature] = 13℃

)

[1] = Array

(

[date]= 周二

[dayPictureUrl] =

[nightPictureUrl] = 

[weather]= 多云转阴

[wind]= 东北风3-4级

[temperature] = 24 ~ 9℃

)

[2] = Array

(

[date]= 周三

[dayPictureUrl] =

[nightPictureUrl] = 

[weather]= 中雨转小雨

[wind]= 东北风3-4级

[temperature] = 15 ~ 8℃

)

[3] = Array

(

[date]= 周四

[dayPictureUrl] =

[nightPictureUrl] =

[weather]= 多云转晴

[wind]= 北风3-4级

[temperature] = 14 ~ 6℃

)

)

)

)

)

meta charset=”UTF-8″

Array

(

   [error] = 0

   [status] = success

   [date] = 2014-03-17

   [results] = Array

       (

           [0] = Array

               (

                   [currentCity]= 上海

                   [weather_data]= Array

                       (

                           [0]= Array

                               (

                                  [date] = 周一(今天, 实时:19℃)

                                  [dayPictureUrl] =

                                  [nightPictureUrl] =

                                   [weather] = 晴

                                  [wind] = 西南风3-4级

                                  [temperature] = 13℃

                               )

                           [1] = Array

                               (

                                   [date]= 周二

                                  [dayPictureUrl] =

                                  [nightPictureUrl] =

                                   [weather]= 多云转阴

                                   [wind]= 东北风3-4级

                                  [temperature] = 24 ~ 9℃

                               )

                           [2] = Array

                               (

                                   [date]= 周三

                                  [dayPictureUrl] =

                                  [nightPictureUrl] =

                                   [weather]= 中雨转小雨

                                   [wind]= 东北风3-4级

                                  [temperature] = 15 ~ 8℃

                               )

                           [3] = Array

                               (

                                   [date]= 周四

                                  [dayPictureUrl] =

                                   [nightPictureUrl] =

                                   [weather]= 多云转晴

                                   [wind]= 北风3-4级

                                  [temperature] = 14 ~ 6℃

                               )

                       )

               )

       )

)

7、PHP中自带了处理json格式字符串的内置函数,下面做一个事例,并给出完整代码:

[php] view plain copy print?

metacharset=”UTF-8″

?php

//城市名

$city = ‘上海’;

//获取json格式的数据

$str = file_get_contents(“”.$city.”output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ”);

//对json格式的字符串进行编码

$arr = json_decode($str,TRUE);

echo “城市:”.$arr[‘results’][0][‘currentCity’].” 日期:”.$arr[‘date’].”br /br /”;

foreach($arr[‘results’][0][‘weather_data’]as $val)

{

echo $val[‘date’].”br/”;

echo “天气:{$val[‘weather’]}br/”;

echo “风向:{$val[‘wind’]}br/”;

echo “温度:{$val[‘temperature’]}br/br /”;

}

?

metacharset=”UTF-8″

?php

   //城市名

   $city = ‘上海’;

 

   //获取json格式的数据

   $str = file_get_contents(“”.$city.”output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ”);

   //对json格式的字符串进行编码

   $arr = json_decode($str,TRUE);      

   echo “城市:”.$arr[‘results’][0][‘currentCity’].” 日期:”.$arr[‘date’].”br /br /”;

   foreach($arr[‘results’][0][‘weather_data’]as $val)

   {

       echo $val[‘date’].”br/”;

       echo “天气:{$val[‘weather’]}br/”;

       echo “风向:{$val[‘wind’]}br/”;

       echo “温度:{$val[‘temperature’]}br/br /”;

   }

?

8、返回的内容如下:

调用百度天气的api时候跨域的问题

同源的问题,目前网上没有很好的解决百度天气api的方案。个人目前实行的方式是,先将api数据的通过file_get_contents获取JSON字符串,然后再用ajax调用.php函数file_get_contents应对百度天气接口

百度首页的天气预报显示用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);

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);

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周

$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怎么解析天气预报api返回的数据

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

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/236297.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 11:59
下一篇 2024-12-12 11:59

相关推荐

  • PHP和Python哪个好找工作?

    PHP和Python都是非常流行的编程语言,它们被广泛应用于不同领域的开发中。但是,在考虑择业方向的时候,很多人都会有一个问题:PHP和Python哪个好找工作?这篇文章将从多个方…

    编程 2025-04-29
  • 使用ActivityWeatherBinding简化天气应用程序的开发

    如何使用ActivityWeatherBinding加快并简化天气应用程序的开发?本文将从以下几个方面进行详细阐述。 一、简介 ActivityWeatherBinding是一个在…

    编程 2025-04-29
  • 掌握magic-api item.import,为你的项目注入灵魂

    你是否曾经想要导入一个模块,但却不知道如何实现?又或者,你是否在使用magic-api时遇到了无法导入的问题?那么,你来到了正确的地方。在本文中,我们将详细阐述magic-api的…

    编程 2025-04-29
  • PHP怎么接币

    想要在自己的网站或应用中接受比特币等加密货币的支付,就需要对该加密货币拥有一定的了解,并使用对应的API进行开发。本文将从多个方面详细阐述如何使用PHP接受加密货币的支付。 一、环…

    编程 2025-04-29
  • 使用PHP foreach遍历有相同属性的值

    本篇文章将介绍如何使用PHP foreach遍历具有相同属性的值,并给出相应的代码示例。 一、基础概念 在讲解如何使用PHP foreach遍历有相同属性的值之前,我们需要先了解几…

    编程 2025-04-28
  • Vertx网关:高效率的API网关中心

    Vertx是一个基于JVM的响应式编程框架,是最适合创建高扩展和高并发应用程序的框架之一。同时Vertx也提供了API网关解决方案,即Vertx网关。本文将详细介绍Vertx网关,…

    编程 2025-04-28
  • Elasticsearch API使用用法介绍-get /_cat/allocation

    Elasticsearch是一个分布式的开源搜索和分析引擎,支持全文检索和数据分析,并且可伸缩到上百个节点,处理PB级结构化或非结构化数据。get /_cat/allocation…

    编程 2025-04-28
  • 解析Azkaban API Flow执行结果

    本文将从多个方面对Azkaban API Flow执行结果进行详细阐述 一、Flow执行结果的返回值 在调用Azkaban API的时候,我们一般都会通过HTTP请求获取Flow执…

    编程 2025-04-27
  • PHP获取301跳转后的地址

    本文将为大家介绍如何使用PHP获取301跳转后的地址。301重定向是什么呢?当我们访问一个网页A,但是它已经被迁移到了另一个地址B,此时若服务器端做了301重定向,那么你的浏览器在…

    编程 2025-04-27
  • PHP登录页面代码实现

    本文将从多个方面详细阐述如何使用PHP编写一个简单的登录页面。 1. PHP登录页面基本架构 在PHP登录页面中,需要包含HTML表单,用户在表单中输入账号密码等信息,提交表单后服…

    编程 2025-04-27

发表回复

登录后才能评论