{ "status": "1", "count": "1", "info": "OK", "infocode": "10000", "lives": [ "0": { "province": "北京", "city": "东城区", "adcode": "110101", "weather": "阴", "temperature": "27", "winddirection": "西北", "windpower": "≤3", "humidity": "81", "reporttime": "2001-07-20 16:23:16" } ] }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2019/7/20 17:50 */ //---------------------------------- // 天气查询 调用类 //---------------------------------- class freeApi{ private $apiUrl = 'https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=<用户key>'; /** * 获取 天气查询 结果 * @return array */ public function getResult(){ return $this->freeApiCurl($this->apiUrl); } /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ public function freeApiCurl($url,$params=false,$ispost=0){ $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'chuanshuoapi' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , 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) { return false; } curl_close( $ch ); return $response; } }