@Asimavi; hocam ufak bi' örnek hazırladım. bu size yardımcı olacaktır.
fonksiyonlar.php;
<?php
function curl_get_contents($url)
{
if(extension_loaded("curl"))
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_NOBODY, false);
curl_setopt($handle, CURLOPT_BINARYTRANSFER, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_TIMEOUT, 10);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
$response = (object) array(
"body" => curl_exec($handle),
"info" => curl_getinfo($handle),
"error" => (object) array(
"ID" => curl_errno($handle),
"message" => curl_error($handle)
)
);
return $response;
}
else
{
return false;
}
}
function get_weather_by_province($province)
{
$url = "http://www.havadurumu.com.tr/havadurumu/{$province}";
$request = curl_get_contents($url);
$response = $request->body;
$name = preg_match(
"#\<h2>(.*?)\<\/h2\>#si",
$response,
$matches
) ? trim(end($matches)) : null;
$tempature = preg_match(
"#\<span.*class\=\"cur\_temp\">(.*?)\<\/span\>#si",
$response,
$matches
) ? trim(end($matches)) : null;
$data_update_time = preg_match(
"#\<span.*class\=\"txt\_graynote\".*\>(G|g)üncelleme\:(.*?)\<\/span\>#si",
$response,
$matches
) ? trim(end($matches)) : null;
$weather_image = preg_match(
"#<div\sclass\=\"box_silver_down(.*?)>(.*?)src\=\"(.*?)\"(.*?)alt=\"(.*?)\".*class\=\"right\"#si",
$response,
$matches
) ? array("url" => "http://www.havadurumu.com.tr/" . trim(trim($matches[3]), "/"), "status" => $matches[5]) : null;
return (object) array(
"name" => $name,
"province" => $province,
"tempature" => (object) array(
"deg" => $tempature,
"status" => $weather_image["status"]
),
"data_update_time" => $data_update_time,
"weather_image" => $weather_image["url"],
);
}index.php;
<?php
# Karakter hatalarını gidermek için dosyamızı
# UTF-8 formatı dönüştürelim.
header("Content-Type: text/html; charset=UTF-8");
# "fonksiyonlar.php" dosyamızı dahil edelim.
require("fonksiyonlar.php");
# İl veya İlçe
$province = "manisa/akhisar";
# Hava durumu bilgisini alalım.
$result = get_weather_by_province($province);
# Sonuçları ekrana yazalım.
?><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title><?=$result->name;?> Hava Durumu</title>
</head>
<body>
<h1><?=$result->name;?> Hava Durumu</h1>
<h2>Güncellenme: <time><?=$result->data_update_time;?></time></h2>
<h3><img src="<?=$result->weather_image;?>" title="<?=$result->tempature->status;?>" /></h3>
<h4><?=$result->tempature->deg;?> <?=$result->tempature->status;?></h4>
</body>
</html>Çalıştırdığımızda ise şöyle bi' sonuç bizi karşılıyor;