免費的獲取最近幾天內的天氣情況

現在很多比較好用的天氣接口都是收費的,自己寫了一個可以通過ip獲取當前城市並且調取天氣信息的函數,用的是新浪的接口,借用了網上的一些資源,現在還能用,這裏面還包括了獲取客戶端ip,將xml轉換爲數組函數
備註:新浪的接口的城市名是需要gb2312編碼的,所以調取接口的時候要先編碼一下

function vget($url){ // 模擬獲取內容函數     
  $curl = curl_init(); // 啓動一個CURL會話     
  curl_setopt($curl, CURLOPT_URL, $url); // 要訪問的地址                 
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對認證證書來源的檢查     
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 從證書中檢查SSL加密算法是否存在     
  curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模擬用戶使用的瀏覽器     
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉     
  curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設置Referer     
  curl_setopt($curl, CURLOPT_HTTPGET, 1); // 發送一個常規的Post請求     
  //curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file); // 讀取上面所儲存的Cookie信息     
  curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設置超時限制防止死循環     
  curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區域內容     
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回     
  $tmpInfo = curl_exec($curl); // 執行操作     
  if (curl_errno($curl)) {     
     //echo '錯誤返回'.curl_error($curl);     
  }     
  curl_close($curl); // 關閉CURL會話     
  return $tmpInfo; // 返回數據     
 }
/**
 * 獲取客戶端IP地址
 * @param integer $type 返回類型 0 返回IP地址 1 返回IPV4地址數字
 * @return mixed
 */
function get_client_ip($type = 0) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos    =   array_search('unknown',$arr);
        if(false !== $pos) unset($arr[$pos]);
        $ip     =   trim($arr[0]);
    }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip     =   $_SERVER['HTTP_CLIENT_IP'];
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法驗證
    $long = ip2long($ip);
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}
/**
   * [getWeather 將xml轉換爲數組]
   * @param  string $xml [description]
   * @return [type]      [description]
   */
  function xml2arr($xml){
      $obj  = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
      $json = json_encode($obj);
      $arr  = json_decode($json, true);
      return $arr;
  }
  /**
   * [getWeather 通過ip獲取城市地址]
   * @param  string $ip [description]
   * @return [type]      [description]
   */
  function getIpCity($ip){
    if(!$ip) $ip = get_client_ip();  //獲取客戶端IP
    $ip = '123.123.123.123';
    //通過ip獲取城市並存儲session
    $ip_url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=".$ip;
    $ip_info = json_decode(vget($ip_url));
    $city = $ip_info->city;
    if(session('weather_city') == '' && $city != ''){
      session('weather_city',$city);
    }
    return $city;
  }
  /**
   * [getWeather 通過城市獲取未來三天天氣]
   * @return [type]      [description]
   */
  function getWeather()
  {
    $city = getIpCity();
    $list = array();
    if($city != ''){
      for($i = 0;$i<3; $i++){
        $weather_url = "http://php.weather.sina.com.cn/xml.php?city=" . iconv("utf-8","gb2312",$city) . "&password=DJOYnieT8234jlsK&day=".$i;
        $weather_info = vget($weather_url);
        $weather_info = xml2arr($weather_info);
        $list[$i] = $weather_info['Weather'];
      }
    }
    return $list;
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章