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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

天气API

發布時間:2024/1/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 天气API 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

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

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

2、心知天氣API網址:http://www.thinkpage.cn/?免費版

3、聚合數據API:

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

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

TP5結合聚合數據API查詢天氣

php根據城市查詢天氣情況
看到有人分享java的查詢全國天氣情況的代碼,于是我想分享一個php版本的查詢天氣接口。免費查詢天氣的接口有很多,比如百度的apistore的天氣api接口,我本來想采用這個接口的,可惜今天百度apistore死活打不開了。那就用聚合數據的天氣api接口吧,也是免費的,不過聚合數據的接口申請相對繁瑣。
1、注冊一個聚合數據的賬號
2、實名認證你的賬號
3、申請你需要的api接口
4、申請驗證你的api接口
雖然是繁瑣了很多,不過返回的信息確是非常的豐富。
好了,現在來分享一下,創業tp5中怎么整合進去。

config.php中,配置你的appkey:

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

    common.php中放入請求的方法:

  • <?php
  • ?
  • /**
  • ?*?請求接口返回內容
  • ?*?@param??string?$url?[請求的URL地址]
  • ?*?@param??string?$params?[請求的參數]
  • ?*?@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;
  • }
  • 復制代碼

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

  • <?php
  • //?+----------------------------------------------------------------------
  • //?|?利用聚合數據查詢天氣
  • //?+----------------------------------------------------------------------
  • //?|?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();
  • ????}
  • ????
  • ????/**
  • ?????*?根據城市獲取天氣情況
  • ?????*/
  • ????public?function?getWeatherByCity()
  • ????{
  • ????????$cityName?=?input('param.cityname');
  • ????????$url?=?"http://op.juhe.cn/onebox/weather/query";
  • ????????$appkey?=?config('appkey');
  • ????????
  • ????????$params?=?[
  • ????????????????"cityname"?=>?$cityName,//要查詢的城市,如:溫州、上海、北京
  • ????????????????"key"?=>?$appkey,//應用APPKEY(應用詳細頁查詢)
  • ????????????????"dtype"?=>?"",//返回數據的格式,xml或json,默認json
  • ????????];
  • ????????$paramstring?=?http_build_query($params);
  • ????????
  • ????????$content?=?juhecurl($url,?$paramstring);
  • ????????$result?=?json_decode($content,?true);
  • ????????
  • ????????if(?empty(?$result?)?){
  • ????????????return?json(?['code'?=>?-1,?'data'?=>?'',?'msg'?=>?'請求失敗']?);
  • ????????}
  • ????????
  • ????????if(?'0'?!=?$result['error_code']?){
  • ????????????return?json(?['code'?=>?-2,?'data'?=>?'',?'msg'?=>?$result['error_code']."?:?".$result['reason']]?);
  • ????????}
  • ????????
  • ????????return?json(?['code'?=>?1,?'data'?=>?$result,?'msg'?=>?'success']?);
  • ????}
  • }
  • 復制代碼

    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是導航?結束-->
  • ????<div?class="search?center">
  • ??????<input?class="text-pc"?id="searchbox"?name=""?type="text"?placeholder="請輸入城市名稱"/>
  • ??????<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代表加載的風格,支持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>
  • 復制代碼

    通過瀏覽器訪問頁面如下:


    輸入你要查詢的城市,比如:南京,點擊查詢


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

    轉載于:https://my.oschina.net/afengzxf/blog/875531

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

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

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。