天气+免费api+php(天气免费下载)

网友投稿 293 2023-01-16

本篇文章给大家谈谈天气+免费api+php,以及天气免费下载对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 今天给各位分享天气+免费api+php的知识,其中也会对天气免费下载进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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

最近在做微信公众平台测试时天气+免费api+php,想在里面子菜单上添加查询未来几天(包括今天)天气的功能,就查找天气+免费api+php了下好用的天气预报查询接口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("http://api.map.baidu.com/telematics/v3/weather?location=".$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] =http://api.map.baidu.com/images/weather/day/qing.png

[nightPictureUrl] =http://api.map.baidu.com/images/weather/night/qing.png

[weather] = 晴

[wind] = 西南风3-4级

[temperature] = 13℃

)

[1] = Array

(

[date]= 周二

[dayPictureUrl] =http://api.map.baidu.com/images/weather/day/duoyun.png

[nightPictureUrl] = http://api.map.baidu.com/images/weather/night/yin.png

[weather]= 多云转阴

[wind]= 东北风3-4级

[temperature] = 24 ~ 9℃

)

[2] = Array

(

[date]= 周三

[dayPictureUrl] =http://api.map.baidu.com/images/weather/day/zhongyu.png

[nightPictureUrl] = http://api.map.baidu.com/images/weather/night/xiaoyu.png

[weather]= 中雨转小雨

[wind]= 东北风3-4级

[temperature] = 15 ~ 8℃

)

[3] = Array

(

[date]= 周四

[dayPictureUrl] =http://api.map.baidu.com/images/weather/day/duoyun.png

[nightPictureUrl] =http://api.map.baidu.com/images/weather/night/qing.png

[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] =http://api.map.baidu.com/images/weather/day/qing.png
                                  [nightPictureUrl] =http://api.map.baidu.com/images/weather/night/qing.png
                                   [weather] = 晴
                                  [wind] = 西南风3-4级
                                  [temperature] = 13℃
                               )

                           [1] = Array
                               (
                                   [date]= 周二
                                  [dayPictureUrl] =http://api.map.baidu.com/images/weather/day/duoyun.png
                                  [nightPictureUrl] = http://api.map.baidu.com/images/weather/night/yin.png
                                   [weather]= 多云转阴
                                   [wind]= 东北风3-4级
                                  [temperature] = 24 ~ 9℃
                               )

                           [2] = Array
                               (
                                   [date]= 周三
                                  [dayPictureUrl] =http://api.map.baidu.com/images/weather/day/zhongyu.png
                                  [nightPictureUrl] = http://api.map.baidu.com/images/weather/night/xiaoyu.png
                                   [weather]= 中雨转小雨
                                   [wind]= 东北风3-4级
                                  [temperature] = 15 ~ 8℃
                               )

                           [3] = Array
                               (
                                   [date]= 周四
                                  [dayPictureUrl] =http://api.map.baidu.com/images/weather/day/duoyun.png
                                   [nightPictureUrl] =http://api.map.baidu.com/images/weather/night/qing.png
                                   [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("http://api.map.baidu.com/telematics/v3/weather?location=".$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("http://api.map.baidu.com/telematics/v3/weather?location=".$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、返回的内容如下:


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

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

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

如何使用中国气象局API开发天气APP

前期的准备工作天气+免费api+php
一、申请API(拿好appid和private_key)

二、解读《SmartWeatherAPI<Lite WebAPI版接口使用说明书》

三、准备好areaid、type、date、appid、urlencode($key)(注意天气+免费api+php,这里经加密的key是需要encodeurl之后的才能成为接口链接的一部分)
好了下面的编码开始:
1、从附件中的areaid_list中找到你想要的地方的areaid,并且选择要查询天气的类型
NSString *areaid = @"101010100";
NSString *type =
@"index_f";
/**
* 官方文档更新的数据类型号
* 指数:index_f(基础接口);index_v(常规接口)
3天预报:forecast_f(基础接口);forecast_v(常规接口)
*
*/
2、获得当前天气date
NSDate
*_date = [NSDate date];
NSDateFormatter *dateFormatter =
[[NSDateFormatter alloc] init];
[dateFormatter
setDateFormat:@"yyyyMMddHHmmss"];//注意日期的格式
NSString *date =
[[dateFormatter stringFromDate:_date]
substringToIndex:12];//用到的精确到分,24小时制60分钟制
3、申请的appid,和private_key
NSString *appid =
@"15ds45s13a465s";//这里是楼主随便输入的,瞎编的
NSString *private_key =
@"46s4ds_SmartWeatherAPI_45s44d6";//也是瞎编的
4、算出经过urlencode后的key,这步比较重要,步骤也多,请耐心看完。
在原来的的基础上是在PHP的环境中算出的,代码如下,可在“
http://writecodeonline.com/php/ ”下进行算法的检验
echo
urlencode(base64_encode(hash_hmac('sha1', " http://open.weather.com.cn/data/?areaid=101010100type=index_fdate=201409041509appid=15ds45s13a465s",
"46s4ds_SmartWeatherAPI_45s44d6",
TRUE)));
首先定义得到public_key和API的方法,还有就是对key进行encodeurl操作的方法
注意,这里的方法都是被天气+免费api+php我定义在getTime的类里面,后面是在main中实例化出来的
//获得publicky
- (NSString*)
getPublicKey:(NSString*)areaid :(NSString*)type :(NSString*)date
:(NSString*)appid {

NSString *Key = [[NSString alloc]
initWithFormat:@"http://open.weather.com.cn/data/?areaid=%@type=%@date=%@appid=%@",
areaid, type, [date substringToIndex:12], appid];

return
Key;
}
//获得完整的API
- (NSString*) getAPI:(NSString*)areaid
:(NSString*)type :(NSString*)date :(NSString*)appid :(NSString*)key
{

NSString *API = [[NSString alloc]
initWithFormat:@"http://open.weather.com.cn/data/?areaid=%@type=%@date=%@appid=%@key=%@",
areaid, type, [date substringToIndex:12], [appid substringToIndex:6],
key];
//-------------这里需要主要的是只需要appid的前6位!!!

return
API;

}
//将获得的key进性urlencode操作
- (NSString
*)stringByEncodingURLFormat:(NSString*)_key{
NSString *encodedString
= (__bridge NSString
*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)_key,
nil, (CFStringRef) @"!$'()*+,-./:;=?@_~%#[]",
kCFStringEncodingUTF8);
//由于ARC的存在,这里的转换需要添加__bridge,原因我不明。求大神讲解
return
encodedString;

}
重点来了,在oc下的算法如下,记得把附件的Base64.h
加进来并引用到工程里面
//对publickey和privatekey进行加密
- (NSString *)
hmacSha1:(NSString*)public_key :(NSString*)private_key{

NSData*
secretData = [private_key
dataUsingEncoding:NSUTF8StringEncoding];
NSData* stringData = [public_key
dataUsingEncoding:NSUTF8StringEncoding];

const void* keyBytes =
[secretData bytes];
const void* dataBytes = [stringData
bytes];

///#define CC_SHA1_DIGEST_LENGTH 20 /* digest
length in bytes */
void* outs =
malloc(CC_SHA1_DIGEST_LENGTH);

CCHmac(kCCHmacAlgSHA1, keyBytes,
[secretData length], dataBytes, [stringData length], outs);

//
Soluion 1
NSData* signatureData = [NSData dataWithBytesNoCopy:outs
length:CC_SHA1_DIGEST_LENGTH freeWhenDone:YES];

return
[signatureData
base64EncodedString];

}
这里只是初步算出来的key,还未encodeurl,链接不能被浏览器识别,所以现在经过算法得到的_key还有一步操作才能的到真正的key。
NSString *_key = [getTime hmacSha1:[getTime
getPublicKey:areaid :type :date :appid] :private_key];
NSString *key =
[getTime
stringByEncodingURLFormat:_key];
最后一步了吧!拼接API
NSString *weatherAPI = [getTime getAPI:areaid :type :date
:appid
:key];
//OK,我们的API就可以用啦。
最后,通过API返回的值是JSON文件,通过解析,就能得到我们想要的数据了,下面拿一个开发的接口举例
NSDictionary *weatherDic = [getTime
getWeatherDic:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];

// weatherDic字典中存放的数据也是字典型,从它里面通过键值取值
NSDictionary
*weatherInfo = [weatherDic
objectForKey:@"weatherinfo"];
NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@ -
%@",[newDateOne substringWithRange:NSMakeRange(0, 4)],[newDateOne
substringWithRange:NSMakeRange(4, 2)] ,[newDateOne
substringWithRange:NSMakeRange(6, 2)],[weatherInfo
objectForKey:@"weather"],[weatherInfo objectForKey:@"temp1"],[weatherInfo
objectForKey:@"temp2"]);
输出:2014-09-04 23:40:23.243
WeatherAPP[5688:201108] 今天是 2014-09-04 的天气状况是:晴 17℃ - 30℃
weatherInfo字典里面的内容是---{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"17℃","temp2":"30℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}

各类免费API推荐,再也不怕找不到免费API了

之前写天气+免费api+php了一个免费、稳定天气+免费api+php的天气预报API
https://www.jianshu.com/p/d878b5290c81
反响还不错天气+免费api+php,先把之前收集的其天气+免费api+php他类的免费API总结了一下,好东西是要大家一起分享的!

http://doc.tuling123.com/openapi2/263611

http://lbsyun.baidu.com/index.php?title=webapi

http://ai.baidu.com/
涵盖图像处理、自然语言、语音技术、知识图谱、数据智能、AR、视频技术和深度学习八大方面。看需选择即可。

http://www.faceplusplus.com.cn/
天气+免费api+php:申请试用的API Key可以免费试用他上面的接口

手机号码归属地API接口: https://www.juhe.cn/docs/api/id/11
历史上的今天API接口: https://www.juhe.cn/docs/api/id/63
股票数据API接口: https://www.juhe.cn/docs/api/id/21
全国WIFI接口: https://www.juhe.cn/docs/api/id/18
星座运势接口: https://www.juhe.cn/docs/api/id/58
黄金数据接口: https://www.juhe.cn/docs/api/id/29
语音识别接口: https://www.juhe.cn/docs/api/id/134
周公解梦接口: https://www.juhe.cn/docs/api/id/64
天气预报API接口: https://www.juhe.cn/docs/api/id/73
身份证查询API接口: https://www.juhe.cn/docs/api/id/38
笑话大全API接口: https://www.juhe.cn/docs/api/id/95
邮编查询接口: https://www.juhe.cn/docs/api/id/66
老黄历接口: https://www.juhe.cn/docs/api/id/65
网站安全检测接口: https://www.juhe.cn/docs/api/id/19
手机固话来电显示接口: https://www.juhe.cn/docs/api/id/72
基金财务数据接口: https://www.juhe.cn/docs/api/id/28
成语词典接口: https://www.juhe.cn/docs/api/id/157
新闻头条接口: https://www.juhe.cn/docs/api/id/235
IP地址接口: https://www.juhe.cn/docs/api/id/1
问答机器人接口: https://www.juhe.cn/docs/api/id/112
汇率API接口: https://www.juhe.cn/docs/api/id/80
电影票房接口: https://www.juhe.cn/docs/api/id/44
万年历API接口: https://www.juhe.cn/docs/api/id/177
NBA赛事接口: https://www.juhe.cn/docs/api/id/92

https://developers.douban.com/wiki/?title=guide

淘宝开放平台 http://open.taobao.com/?spm=a219a.7395905.1.1.YdFDV6

微博开放平台 http://open.weibo.com/wiki/API

讯飞语音 http://www.xfyun.cn/robots/solution
马化腾的微信开放平台(对应的还有腾讯开放平台)
https://open.weixin.qq.com/
融云IM https://developer.rongcloud.cn/signin?returnUrl=%2Fapp%2Fappkey%2FPv4vYQwaxSZdfpLX5AI%3D
高德地图 http://lbs.amap.com/ 关于天气+免费api+php和天气免费下载的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。 天气+免费api+php的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于天气免费下载、天气+免费api+php的信息别忘了在本站进行查找喔。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:中天快运物流查询方式(中天物流有限公司官网)
下一篇:中泰金牌物流查询(中泰物流官网)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~