日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

天气API

發(fā)布時(shí)間:2024/1/17 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 天气API 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

1、中國(guó)天氣http://-weather.com.cn (先查城市id 再查天氣)

http://61.4.185.48:81/g/ (根據(jù)IP查詢城市ID)
http://m.weather.com.cn/data/101110101.html(六天預(yù)報(bào))
http://www.weather.com.cn/data/sk/101110101.html(實(shí)時(shí)天氣信息)

2、心知天氣API網(wǎng)址:http://www.thinkpage.cn/?免費(fèi)版

3、聚合數(shù)據(jù)API:

股票行情API https://www.juhe.cn/docs/api/id/21

https://www.juhe.cn/docs/api/id/80

TP5結(jié)合聚合數(shù)據(jù)API查詢天氣

php根據(jù)城市查詢天氣情況
看到有人分享java的查詢?nèi)珖?guó)天氣情況的代碼,于是我想分享一個(gè)php版本的查詢天氣接口。免費(fèi)查詢天氣的接口有很多,比如百度的apistore的天氣api接口,我本來想采用這個(gè)接口的,可惜今天百度apistore死活打不開了。那就用聚合數(shù)據(jù)的天氣api接口吧,也是免費(fèi)的,不過聚合數(shù)據(jù)的接口申請(qǐng)相對(duì)繁瑣。
1、注冊(cè)一個(gè)聚合數(shù)據(jù)的賬號(hào)
2、實(shí)名認(rèn)證你的賬號(hào)
3、申請(qǐng)你需要的api接口
4、申請(qǐng)驗(yàn)證你的api接口
雖然是繁瑣了很多,不過返回的信息確是非常的豐富。
好了,現(xiàn)在來分享一下,創(chuàng)業(yè)tp5中怎么整合進(jìn)去。

config.php中,配置你的appkey:

  • //配置文件
  • return?[
  • ????
  • ????'appkey'?=>?''??//此處填入你的key
  • ];
  • 復(fù)制代碼

    common.php中放入請(qǐng)求的方法:

  • <?php
  • ?
  • /**
  • ?*?請(qǐng)求接口返回內(nèi)容
  • ?*?@param??string?$url?[請(qǐng)求的URL地址]
  • ?*?@param??string?$params?[請(qǐng)求的參數(shù)]
  • ?*?@param??int?$ipost?[是否采用POST形式]
  • ?*?@return??string
  • ?*/
  • function?juhecurl($url,?$params=false,?$ispost=0){
  • ????
  • ????$httpInfo?=?[];
  • ????$ch?=?curl_init();
  • ?
  • ????curl_setopt(?$ch,?CURLOPT_HTTP_VERSION?,?CURL_HTTP_VERSION_1_1?);
  • ????curl_setopt(?$ch,?CURLOPT_USERAGENT?,?'JuheData'?);
  • ????curl_setopt(?$ch,?CURLOPT_CONNECTTIMEOUT?,?60?);
  • ????curl_setopt(?$ch,?CURLOPT_TIMEOUT?,?60);
  • ????curl_setopt(?$ch,?CURLOPT_RETURNTRANSFER?,?true?);
  • ????curl_setopt($ch,?CURLOPT_FOLLOWLOCATION,?true);
  • ????if(?$ispost?)
  • ????{
  • ????????curl_setopt(?$ch?,?CURLOPT_POST?,?true?);
  • ????????curl_setopt(?$ch?,?CURLOPT_POSTFIELDS?,?$params?);
  • ????????curl_setopt(?$ch?,?CURLOPT_URL?,?$url?);
  • ????}
  • ????else
  • ????{
  • ????????if($params){
  • ????????????curl_setopt(?$ch?,?CURLOPT_URL?,?$url.'?'.$params?);
  • ????????}else{
  • ????????????curl_setopt(?$ch?,?CURLOPT_URL?,?$url);
  • ????????}
  • ????}
  • ????$response?=?curl_exec(?$ch?);
  • ????if?($response?===?FALSE)?{
  • ????????//echo?"cURL?Error:?"?.?curl_error($ch);
  • ????????return?false;
  • ????}
  • ????$httpCode?=?curl_getinfo(?$ch?,?CURLINFO_HTTP_CODE?);
  • ????$httpInfo?=?array_merge(?$httpInfo?,?curl_getinfo(?$ch?)?);
  • ????curl_close(?$ch?);
  • ????
  • ????return?$response;
  • }
  • 復(fù)制代碼

    控制器中,index.php的代碼:

  • <?php
  • //?+----------------------------------------------------------------------
  • //?|?利用聚合數(shù)據(jù)查詢天氣
  • //?+----------------------------------------------------------------------
  • //?|?Copyright?(c)?2016~2022?http://baiyf.cn?All?rights?reserved.
  • //?+----------------------------------------------------------------------
  • //?|?Licensed?(?http://www.apache.org/licenses/LICENSE-2.0?)
  • //?+----------------------------------------------------------------------
  • //?|?Author:?NickBai?<1902822973@qq.com>
  • //?+----------------------------------------------------------------------
  • namespace?app\weather\controller;
  • ?
  • use?think\Controller;
  • ?
  • class?Index?extends?Controller
  • {
  • ????public?function?index()
  • ????{
  • ????????return?$this->fetch();
  • ????}
  • ????
  • ????/**
  • ?????*?根據(jù)城市獲取天氣情況
  • ?????*/
  • ????public?function?getWeatherByCity()
  • ????{
  • ????????$cityName?=?input('param.cityname');
  • ????????$url?=?"http://op.juhe.cn/onebox/weather/query";
  • ????????$appkey?=?config('appkey');
  • ????????
  • ????????$params?=?[
  • ????????????????"cityname"?=>?$cityName,//要查詢的城市,如:溫州、上海、北京
  • ????????????????"key"?=>?$appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢)
  • ????????????????"dtype"?=>?"",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)json
  • ????????];
  • ????????$paramstring?=?http_build_query($params);
  • ????????
  • ????????$content?=?juhecurl($url,?$paramstring);
  • ????????$result?=?json_decode($content,?true);
  • ????????
  • ????????if(?empty(?$result?)?){
  • ????????????return?json(?['code'?=>?-1,?'data'?=>?'',?'msg'?=>?'請(qǐng)求失敗']?);
  • ????????}
  • ????????
  • ????????if(?'0'?!=?$result['error_code']?){
  • ????????????return?json(?['code'?=>?-2,?'data'?=>?'',?'msg'?=>?$result['error_code']."?:?".$result['reason']]?);
  • ????????}
  • ????????
  • ????????return?json(?['code'?=>?1,?'data'?=>?$result,?'msg'?=>?'success']?);
  • ????}
  • }
  • 復(fù)制代碼

    view層中,index.html的代碼如下:

  • <!doctype?html>
  • <html>
  • ??
  • ??<head>
  • ????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
  • ????<meta?http-equiv="X-UA-Compatible"?content="IE=edge">
  • ????<title>天氣查詢</title>
  • ????<script?src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"?type="text/javascript"></script>
  • ????<script?src="/static/layer/layer.js"?type="text/javascript"></script>
  • ????<link?href="/static/css/style.css"?rel="stylesheet"?type="text/css">
  • ??<body>
  • ????<!--nav是導(dǎo)航?結(jié)束-->
  • ????<div?class="search?center">
  • ??????<input?class="text-pc"?id="searchbox"?name=""?type="text"?placeholder="請(qǐng)輸入城市名稱"/>
  • ??????<a?class="button-pc"?id="searchBtn"?href="javascript:void(0);">
  • ????????<span?class="icon-search"></span>
  • ????????<span?class="search2?am-hide-sm-only">查詢</span></a>
  • ????</div>
  • ????
  • ????<div?class="check-Result?center"?style="display:block">
  • ????????
  • ????</div>
  • ????
  • ????<script?type="text/javascript">
  • ????????$(function(){
  • ?
  • ????????????$("#searchBtn").click(function(){
  • ????????????????var?city?=?$("#searchbox").val();
  • ????????????????if(?''?==?city?){
  • ????????????????????layer.alert('城市名稱不能為空',?{?'icon'?:?2?});
  • ????????????????????return?;
  • ????????????????}
  • ????????????????var?index?=?layer.load(0,?{shade:?false});?//0代表加載的風(fēng)格,支持0-2
  • ?
  • ????????????????$.getJSON(?"{:url('weather/index/getWeatherByCity')}",?{?'cityname'?:?city?},?function(res){
  • ?
  • ????????????????????layer.close(?index?);
  • ????????????????????if(?1?==?res.code?){
  • ????????????????????????
  • ????????????????????}else{
  • ?
  • ????????????????????????layer.alert(?res.msg?,?{?'icon'?:?2?});
  • ????????????????????}
  • ????????????????});
  • ????????????})
  • ????????});
  • ????</script>
  • ????
  • ??</body>
  • </html>
  • 復(fù)制代碼

    通過瀏覽器訪問頁(yè)面如下:


    輸入你要查詢的城市,比如:南京,點(diǎn)擊查詢


    json數(shù)據(jù)成功返回,這是你就可以根據(jù)你的需要渲染頁(yè)面了。參數(shù)的講解參照這里
    https://www.juhe.cn/docs/api/id/73

    轉(zhuǎn)載于:https://my.oschina.net/afengzxf/blog/875531

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

    總結(jié)

    以上是生活随笔為你收集整理的天气API的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。