最佳实践之示例代码

如果你刚开始使用和风天气的API数据,我们提供了以下几种主流语言的调用方式:

有一些单片机或WIFI模块,由于不支持HTTPS,因此无法使用我们的API接口,请谨慎使用

JAVA

根据key值请求


        //参数字符串,如果拼接在请求链接之后,需要对中文进行 URLEncode   字符集 UTF-8
        String param = "key=xxxxxxxxxxxxxxxxxxx&location=北京";
        StringBuilder sb = new StringBuilder();
        InputStream    is=null;
        BufferedReader br=null;
        try {
            //接口地址
            String            url        = "https://api.heweather.com/s6/weather";
            URL               uri        = new URL(url);
            HttpURLConnection connection= (HttpURLConnection) uri.openConnection();
            connection.setRequestMethod("POST");
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(10000);
            connection.setRequestProperty("accept", "*/*");
            //发送参数
            connection.setDoOutput(true);
            PrintWriter out = new PrintWriter(connection.getOutputStream());
            out.print(param);
            out.flush();
            //接收结果
            is = connection.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            String         line;
            //缓冲逐行读取
            while ( (line = br.readLine()) != null ) {
                sb.append(line);
            }
            System.out.println(sb.toString());
        }catch ( Exception ignored ){}
        finally {
            //关闭流
            try {
                if(is!=null){
                    is.close();
                }
                if(br!=null){
                    br.close();
                }
                if (out!=null){
                    out.close();
                }
            } catch (IOException e2) {}
        }

根据sign请求


//参数字符串,如果拼接在请求链接之后,需要对中文进行 URLEncode   字符集 UTF-8
        String params="sign=xxxxxxxxxxxxxxxxxxxx&username=xxxxxxxxxxxxxxxxxxx&location=北京&t=秒级时间戳";
        StringBuilder sb = new StringBuilder();
        InputStream    is=null;
        BufferedReader br=null;
        try{
            //接口地址
            String            url        = "https://api.heweather.com/s6/weather";
            URL               uri        = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
            connection.setRequestMethod("POST");
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(10000);
            connection.setRequestProperty("accept", "*/*");
            //发送参数
            connection.setDoOutput(true);
            PrintWriter out = new PrintWriter(connection.getOutputStream());
            out.print(params);
            out.flush();
            //接收结果
             is= connection.getInputStream();
             br= new BufferedReader(new InputStreamReader(is));
            String         line;
            //缓冲逐行读取
            while ( (line = br.readLine()) != null ) {
                sb.append(line);
            }
        }catch ( Exception ignored ){}
        finally {
            //输出结果
            out.println(sb.toString());
            //关闭流
            try {
                if(is!=null){
                    is.close();
                }
                if(br!=null){
                    br.close();
                }
                if(out!=null){
                    out.close();
                }
            } catch (IOException e2) {}
        }
    }

PHP

根据KEY值请求


    function requestByKey(){
        //准备请求参数
        $key ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        $location = "北京";
        $curlPost = "key=".$key."&location=".urlencode($location);
        //初始化请求链接
        $req=curl_init();
        //设置请求链接
        curl_setopt($req, CURLOPT_URL,'https://free-api.heweather.com/s6/weather/now?'.$curlPost);
        //设置超时时长(秒)
        curl_setopt($req, CURLOPT_TIMEOUT,3);
        //设置链接时长
        curl_setopt($req, CURLOPT_CONNECTTIMEOUT,10);
        //设置头信息
        $headers=array( "Accept: application/json", "Content-Type: application/json;charset=utf-8" );
        curl_setopt($req, CURLOPT_HTTPHEADER, $headers);
        
        curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($req, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($req);
        curl_close($req);
        return $data;
    }


根据sign请求


function requestBySign(){
        //准备请求参数(需要替换)
        $key ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        $params['location']='北京';
        //(需要替换)
        $params['username']='YYYYYYYYYYYYYYYYYYYYY';
        $params['t']=time();
        $sign = getSignature($params, $key);
        $curlPost = "sign=".$sign."&location=".urlencode($params['location'])."&username=".$params['username']."&t=".$params['t'];
        var_dump($curlPost);
        //初始化请求链接
        $req=curl_init();
        //设置请求链接
        curl_setopt($req, CURLOPT_URL,'https://free-api.heweather.com/s6/weather/now?'.$curlPost);
        //设置超时时长(秒)
        curl_setopt($req, CURLOPT_TIMEOUT, 3);
        
        //设置链接时长
        curl_setopt($req, CURLOPT_CONNECTTIMEOUT, 10);
        
        //设置头信息
        $headers=array( "Accept: application/json", "Content-Type: application/json;charset=utf-8" );
        curl_setopt($req, CURLOPT_HTTPHEADER, $headers);
        
        curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($req, CURLOPT_SSL_VERIFYHOST, false);
        
        $data = curl_exec($req);
        curl_close($req);
        return $data;
    }