php调用阿里云天气预报

weather.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>天气查询</title>
</head>
<body>
    <form action="weather.php" method="get">
    <input type="text" name="area">
    <input type="submit" name="查询">
    </form>
</body>
</html>
<input type="text" name="area"> area传值到php页面  想要查询天气的地名建一个weather.php文件内容:
<?php
     header("Content-Type:text/html;charset=UTF-8");
     date_default_timezone_set("PRC");
    // $host = "http://ali-weather.showapi.com/ip-to-weather";    //通过ip地址查询天气
    $host = "http://ali-weather.showapi.com/area-to-weather";   //通过地区名字查询天气
    $method = "GET";
    $appcode = "ff90fe45686e4cb8b3e260f2692798b4";//需要到阿里云购买服务,获取appcode
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    array_push($headers, "Content-Type: application/x-www-form-urlencoded; charset=utf-8");
    $querys="area=".$_GET[‘area‘];//获取html页面传递过来的地区名字
    $bodys = "";
    $url = $host . "?" . $querys;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
    $response = curl_exec($curl);
    $header_size    = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    //echo  $header_size;
    $headers        = substr($response, 0, $header_size);
    //echo  $headers;$body    = substr($response, $header_size);
    //解析 $body对象 $weather=json_decode($body);

echo  "<img src=".$weather->showapi_res_body->f1->day_weather_pic." /><br>";
echo  "城市:  ".$weather->showapi_res_body->cityInfo->c5."<br/>";
echo  "白天天气:  ".$weather->showapi_res_body->f1->day_weather."<br/>";
echo  "晚上天气:  ".$weather->showapi_res_body->f1->night_weather."<br/>";
echo  "最低温度:  ".$weather->showapi_res_body->f1->night_air_temperature."<br/>";
echo  "最高温度:  ".$weather->showapi_res_body->f1->day_air_temperature."<br/>";
 echo  "PM2.5:  ".$weather->showapi_res_body->now->aqiDetail->pm2_5." ".$weather->showapi_res_body->now->aqiDetail->quality."<br/>";
 ?>    

效果截图

难点:主要在解析json返回的格式上面

$body 返回数据:

{"showapi_res_code":0,"showapi_res_error":"","showapi_res_body":{"time":"20170811073000","ret_code":0,"now":{"aqiDetail":{"co":"0.764","num":"166","so2":"7","area":"杭州","o3":"9","no2":"44","quality":"优质","aqi":"47","pm10":"46","pm2_5":"27","o3_8h":"19","primary_pollutant":""},"weather_code":"01","temperature_time":"08:30","wind_direction":"南风","wind_power":"2级","sd":"74%","aqi":"47","weather":"多云","weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","temperature":"29"},"cityInfo":{"c6":"zhejiang","c5":"杭州","c4":"hangzhou","c3":"杭州","c9":"中国","c8":"china","c7":"浙江","c17":"+8","c16":"AZ9571","c1":"101210101","c2":"hangzhou","longitude":120.165,"c11":"0571","latitude":30.319,"c10":"1","c12":"310000","c15":"43"},"f1":{"day_weather":"多云","night_weather":"多云","night_weather_code":"01","air_press":"1004 hPa","jiangshui":"2%","night_wind_power":"微风 <5.4m/s","day_wind_power":"微风 <5.4m/s","day_weather_code":"01","sun_begin_end":"05:23|18:45","ziwaixian":"很强","day_weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","weekday":5,"night_air_temperature":"27","day_air_temperature":"36","day_wind_direction":"东南风","day":"20170811","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","night_wind_direction":"东南风"},"f3":{"day_weather":"多云","night_weather":"多云","night_weather_code":"01","air_press":"1004 hPa","jiangshui":"1%","night_wind_power":"3-4级 5.5~7.9m/s","day_wind_power":"3-4级 5.5~7.9m/s","day_weather_code":"01","sun_begin_end":"05:25|18:44","ziwaixian":"很强","day_weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","weekday":7,"night_air_temperature":"28","day_air_temperature":"37","day_wind_direction":"东南风","day":"20170813","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","night_wind_direction":"西南风"},"f2":{"day_weather":"雷阵雨","night_weather":"多云","night_weather_code":"01","air_press":"1004 hPa","jiangshui":"82%","night_wind_power":"3-4级 5.5~7.9m/s","day_wind_power":"微风 <5.4m/s","day_weather_code":"04","sun_begin_end":"05:24|18:45","ziwaixian":"很强","day_weather_pic":"http://app1.showapi.com/weather/icon/day/04.png","weekday":6,"night_air_temperature":"27","day_air_temperature":"32","day_wind_direction":"东南风","day":"20170812","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","night_wind_direction":"东南风"},"showapi_treemap":true}}

对于新手需要调整好json格式才好进行解析 ,在ide工具上按照{ } 和  ,进行一个层次拆分 然后就比较好解析 记得缩进。

时间: 2024-10-11 22:24:41

php调用阿里云天气预报的相关文章

Python 调用阿里云 API 收集 ECS 数据

#!/usr/bin/env python # coding: utf-8 # author: Wang XiaoQiang ''' 功能介绍: 1.调用阿里云API,收集所有区域 ECS 信息 2.将需要的数据整理.生成 Excel 文档 3.关于阿里 sdk 的安装,api 的调用请参考阿里云官网 4.xlsxwriter 请参考这里:http://xlsxwriter.readthedocs.org/ ''' import json, sys try: from termcolor imp

零基础大数据入门教程:Java调用阿里云短信通道服务

这里我们使用SpringBoot 来调用阿里通信的服务. 阿里通信,双11.收到短信,日发送达6亿条.保障力度非常高. 使用的步骤: 1.1. 第一步:需要开通账户 1.2. 第二步:阅读接口文档 1.2.1. 秘钥管理 1.2.2. 短信签名 1.2.3. 短信模板 1.3. SDK 这个由阿里云提供. 编译与打包. 打包到本地仓库,或者公司局域网内的私服地址. Maven打包 1.4. 第三步:创建SpringBoot工程,导入依赖 <!-- sms单独打包 --> <depende

Powershell 调用阿里云 云解析API 实现动态域名解析

由于阿里云解析API调用官方文档中没有Powershell的示例脚本,而API接口调用实际是通过向DNS API的服务端地址发送HTTP POST或GET请求,因此根据官方文档写了相关的函数用于查询域名解析.修改域名解析的状态.如果要增删域名解析,参考官方文档修改函数中的Action等参数即可. 参考的阿里API调用链接:https://help.aliyun.com/document_detail/29743.html?spm=a2c4g.11186623.6.614.35f94c7bRwGb

调用阿里云短信服务

package com.example.demo.untils; /** * Created by JQY on 2019/5/15 */ import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.Send

调用阿里云OSS未释放资源造成的内存溢出

前段时间线上服务频繁出现内存溢出的问题,有时候半夜就会挂掉,运维同事还得从睡梦中爬起来重启,给我们带来很大的困扰.一次运维同事在快到挂掉前把jvm 的heap dump拿了下来给了我们,通过内存分析工具 MemoryAnalyzer 打开看到结果如下 可以看到有大量的org.apache.http.impl.conn.PoolingHttpClientConnectionManager 对象未被释放 ,并且可以看到是 阿里云的 oss 在引用的,灵机一动,可能是访问oss服务的时候某些资源没被释

调用阿里云api获取阿里云数据同步服务(DTS)并且作图发送邮件的整个流程

前言 在https://rorschachchan.github.io/2018/02/24/阿里云获取DTS服务延迟的脚本/ 文章里已经写过,领导现在要求"每天查看阿里云dts同步的延迟情况和同步速率情况",并且在https://rorschachchan.github.io/2018/02/27/使用matplotlib画图的一个脚本/ 里面也放了一个使用python matplotlib画图的demo,这篇文章的目的就是把整个过程实现,并且把dts图形以每日邮件的形式发送给领导的

python3.6 通过调用 阿里云 API (非SDK方式) 查询 可用区 例子

#!/usr/bin/env python3.5 # -*- coding:utf8 -*- try: import httplib except ImportError: import http.client as httplib import sys, datetime import urllib import urllib.request import urllib.error import urllib.parse import time import json import base6

阿里云自定义监控

自定义监控:对上面监控的补充,可以自定义相应的监控项,在服务器上执行相应的脚本采集数据,然后调用阿里云封装的JDK将数据上传,进行报警处理. 下载阿里云的JDK到服务器相应的目录下 http://help.aliyun.com/knowledge_detail.htm?knowledgeId=5974901 /usr/local/aegis/aegis_quartz/aegis_quartz/libexec/user 添加自定义的选项 报警规则: 这里要注意地段后填写的内容,这个是与上传的字段匹

开源PaaS工具CloudFoundry落地阿里云

原文:https://yq.aliyun.com/articles/292815?utm_content=m_37457 云计算技术的不断成熟和完善,尤其是IaaS平台的不断发展,使得越来越多的企业和用户青睐于将自己的业务和应用不断的从传统IT设施迁移到云上,在灵活.高效管理应用,快速扩展业务的同时不断地降低基础设施的运维和管理成本.然而,随着业务的不断发展和壮大,对IaaS资源管理成本也会不断增加,可否将基础设施的管理成本进一步降低呢?Cloud Foudry就是其中一个非常完美的解决方案,作