Bir proje için yazdığım, havadurumu.com.tr üzerinden veri çeken ufak bir uygulama. Sizinde işinize yarayabilir.
Weather.php (Class Dosyası) <?php
class Weather {
function __construct( $city = null ){
$this->url = 'http://www.havadurumu.com.tr/havadurumu/';
$this->city = $city;
}
function get_data(){
$curldata = curl_init();
$options = array(
CURLOPT_URL => $this->url . $this->city,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => "spider",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
);
curl_setopt_array($curldata, $options);
$data = curl_exec($curldata);
curl_close($curldata);
return $data;
}
function parse_data(){
$parse = $this->get_data();
preg_match_all('#<span class="cur_temp">(.*?)</span>#si',$parse,$degree);
preg_match_all('#<img src="/icons/(.*?)".*?alt="(.*?)".*?title="(.*?)" />#si',$parse, $detail);
$this->set_degree($degree[0][0]);
$this->set_image($detail[0][0]);
$this->set_condition($detail[2][0]);
}
function set_degree( $degree ){
$this->degree = $degree;
}
function get_degree(){
return $this->degree;
}
function set_image( $image ){
$this->image = $image;
}
function get_image(){
return $this->image;
}
function set_condition( $cond ){
$this->condition = $cond;
}
function get_condition(){
return $this->condition;
}
}
?>Kullanımı (veri.php) <?php
include_once 'weather.php';
if(!empty($_GET['c']))
$city = $_GET['c'];
else
$city = 'izmir';
$Weather = new Weather($city);
$Weather->parse_data();
$degree = $Weather->degree;
$city = $Weather->city;
$condit = $Weather->condition;
$image = $Weather->image;
?>
kodu
veri.php şeklinde çağırdığınızda otomatik olarak izmir havadurumunu getirecektir.
veri.php?c=ankara yada veri.php?c=ankara/kizilay olarak çağırdığınızda yazdığınız şehrin ve semtin havadurumunu getirecektir.
condition = metin olarak hava durumunu verir (örn: parçalı bulutlu)
image = hava durumu görselini verir.
degree = dereceyi getirir.
city = çağırılan şehri.
kolay gelsin.