PHP 根据城市获取天气信息 阿里云接口

//根据城市获取天气信息
function weather_api($city){
    $host = "http://jisutqybmf.market.alicloudapi.com";//接口地址
    $path = "/weather/query";//接口
    $method = "GET";//请求方法
    $appcode = "";//阿里云appcode
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "city=".$city;
    $bodys = "";
    $url = $host . $path . "?" . $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);
    }
    $weather=curl_exec($curl);
    $start_str=stripos($weather,'{');
    $end_str=strripos($weather,'}');
    $weather_s=json_decode(substr($weather,$start_str,$end_str-$start_str+1),true);
    if(is_array($weather_s)){
        return $weather_s;
    }else{
        return false;
    }


}

你可能感兴趣的:(PHP 根据城市获取天气信息 阿里云接口)