基于PHP的聚合数据天气预报api调用示例.doc_第1页
基于PHP的聚合数据天气预报api调用示例.doc_第2页
基于PHP的聚合数据天气预报api调用示例.doc_第3页
基于PHP的聚合数据天气预报api调用示例.doc_第4页
基于PHP的聚合数据天气预报api调用示例.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于PHP的聚合数据天气预报api调用示例本代码示例是基于PHP的聚合数据天气预报api服务请求的代码样例,使用前你需要:通过/docs/api/id/39 申请一个天气预报API的appkey样例代码包含了获取支持城市列表、根据城市获取天气预报、根据IP地址请求天气预报、根据GPS坐标请求天气、城市3小时天气预报的实现。示例代码主要是解析一些常用字段,如需要完整或其他未包含的字段,可以自行参考官方的接口,进行修改。首先:引入封装好的天气调用类header(Content-type:text/html;charset=utf-8);includeclass.juhe.weather.php;/引入天气请求类/接口基本信息配置$appkey=*;/您申请的天气查询appkey$weather=newweather($appkey);一、获取支持的城市列表由于支持的城市列表基本不会这么变化,大家可以获取到列表后内置到自己的应用中,就不用每次都去请求API。$citysResult=$weather-getCitys();if($citysResulterror_code=0)/以下可根据实际业务需求,自行改写/$citys=$citysResultresult;foreach($citysas$ckey=$c)echoID:.$cid.,省份:.$cprovince.,城市:.$ccity.,区域:.$cdistrict.;else/以下可根据实际业务需求,自行改写/echo$citysResulterror_code.:.$citysResultreason;二、根据城市/ID获取天气预报通过城市的名称或城市的ID来获取天气预报,城市id就是获取城市支持列表中返回的字段ID$cityWeatherResult=$weather-getWeather(苏州);if($cityWeatherResulterror_code=0)/以下可根据实际业务需求,自行改写/$data=$cityWeatherResultresult;echo=当前天气实况=;echo温度:.$datasktemp.;echo风向:.$dataskwind_direction.(.$dataskwind_strength.);echo湿度:.$dataskhumidity.;echo;echo=未来几天天气预报=;foreach($datafutureas$wkey=$f)echo日期:.$fdate.$fweek.$fweather.$ftemperature.;echo;echo=相关天气指数=;echo穿衣指数:.$datatodaydressing_index.,.$datatodaydressing_advice.;echo紫外线强度:.$datatodayuv_index.;echo舒适指数:.$datatodaycomfort_index.;echo洗车指数:.$datatodaywash_index;echo;elseecho$cityWeatherResulterror_code.:.$cityWeatherResultreason;三、根据用户的IP地址请求对应的天气预报通过用户的IP地址获取用户所在地的天气预报,由于IP地址解析可能会有误差,所以有时定位到的城市不一定是用户实际的所在地。$ipWeatherResult=$weather-getWeatherByIP(28);if($ipWeatherResulterror_code=0)/以下可根据实际业务需求,自行改写/$data=$ipWeatherResultresult;echo=当前城市=;echo$datatodaycity;echo;echo=当前天气实况=;echo温度:.$datasktemp.;echo风向:.$dataskwind_direction.(.$dataskwind_strength.);echo湿度:.$dataskhumidity.;echo;echo=未来几天天气预报=;foreach($datafutureas$wkey=$f)echo日期:.$fdate.$fweek.$fweather.$ftemperature.;echo;echo=相关天气指数=;echo穿衣指数:.$datatodaydressing_index.,.$datatodaydressing_advice.;echo紫外线强度:.$datatodayuv_index.;echo舒适指数:.$datatodaycomfort_index.;echo洗车指数:.$datatodaywash_index;echo;elseecho$ipWeatherResulterror_code.:.$ipWeatherResultreason;四、根据GPS坐标来获取对应地区的天气无论通过二、三、四获取的天气预报,因为聚合格式都是统一的,所以解析的流程是一致的,所以没有额外的操作,只是传参上有点的差异。$geoWeatherResult=$weather-getWeatherByGeo(116.401394,39.916042);if($geoWeatherResulterror_code=0)/以下可根据实际业务需求,自行改写/$data=$geoWeatherResultresult;echo=当前城市=;echo$datatodaycity;echo;echo=当前天气实况=;echo温度:.$datasktemp.;echo风向:.$dataskwind_direction.(.$dataskwind_strength.);echo湿度:.$dataskhumidity.;echo;echo=未来几天天气预报=;foreach($datafutureas$wkey=$f)echo日期:.$fdate.$fweek.$fweather.$ftemperature.;echo;echo=相关天气指数=;echo穿衣指数:.$datatodaydressing_index.,.$datatodaydressing_advice.;echo紫外线强度:.$datatodayuv_index.;echo舒适指数:.$datatodaycomfort_index.;echo洗车指数:.$datatodaywash_index;echo;elseecho$geoWeatherResulterror_code.:.$geoWeatherResultreason;五、获取城市三小时预报就是城市每3小时的天气情况$forecastResult=$weather-getForecast(苏州);if($forecastResulterror_code=0)/以下可根据实际业务需求,自行改写/$data=$forecastResultresult;foreach($dataas$key=$d)echo日期:.$ddate.(.$dsh.点-.$deh.点).$dweather.$dtemp1.$dtemp2.;else/以下可根据实际业务需求,自行改写/echo$forecastResulterror_code.:.$forecastResultreason;通过上面的示例代码,大家应该对如果调用聚合数据天气预报API有了一个大体的了解。最后放上class.juhe.weather.php完整代码:!-?php/+-/|JuhePHPNOZUONODIE/+-/|Copyright(c)2010-2015Allrightsreserved./+-/|Author:Juhedata/+-/-/聚合数据天气预报接口请求类/-classweatherprivate$appkey=false;/申请的聚合天气预报APPKEYprivate$cityUrl=/weather/citys;/城市列表APIURLprivate$weatherUrl=/weather/index;/根据城市请求天气APIURLprivate$weatherIPUrl=/weather/ip;/根据IP地址请求天气APIURLprivate$weatherGeoUrl=/weather/geo;/根据GPS坐标获取天气APIURLprivate$forecast3hUrl=/weather/forecast3h;/获取城市天气3小时预报APIURLpublicfunction_construct($appkey)$this-appkey=$appkey;/*获取天气预报支持城市列表*returnarray*/publicfunctiongetCitys()$params=key=.$this-appkey;$content=$this-juhecurl($this-cityUrl,$params);return$this-_returnArray($content);/*根据城市名称/ID获取详细天气预报*paramstring$city城市名称/ID*returnarray*/publicfunctiongetWeather($city)$paramsArray=array(key=$this-appkey,cityname=$city,format=2);$params=http_build_query($paramsArray);$content=$this-juhecurl($this-weatherUrl,$params);return$this-_returnArray($content);/*根据IP地址获取当地天气预报*paramstring$ipIP地址*returnarray*/publicfunctiongetWeatherByIP($ip)$paramsArray=array(key=$this-appkey,ip=$ip,format=2);$params=http_build_query($paramsArray);$content=$this-juhecurl($this-weatherIPUrl,$params);return$this-_returnArray($content);/*根据GPS坐标获取当地的天气预报*paramstring$lon经度*paramstring$lat纬度*returnarray*/publicfunctiongetWeatherByGeo($lon,$lat)$paramsArray=array(key=$this-appkey,lon=$lon,lat=$lat,format=2);$params=http_build_query($paramsArray);$content=$this-juhecurl($this-weatherGeoUrl,$params);return$this-_returnArray($content);/*获取城市三小时预报*paramstring$city城市名称*returnarray*/publicfunctiongetForecast($city)$paramsArray=array(key=$this-appkey,cityname=$city,format=2);$params=http_build_query($paramsArray);$content=$this-juhecurl($this-forecast3hUrl,$params);return$this-_returnArray($content);/*将JSON内容转为数据,并返回*paramstring$content内容*returnarray*/publicfunction_returnArray($content)returnjson_decode($content,true);/*请求接口返回内容*paramstring$url请求的URL地址*paramstring$params请求的参数*paramint$ipost是否采用POST形式*returnstring*/publicfunctionjuhecurl($url,$params=false,$ispost=0)$httpInfo=array();$ch=curl_init();curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);curl_setopt($ch,CURLOPT_USERAGENT,Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeG

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论