الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

php استعمل google api php الارصاد الجوية

بدأه billphigor في 26 سبتمبر 2010 · 0 رد · 410 مشاهدة · في منتدى تطوير المواقع بـ PHP
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم ورحمة الله

نقوم اولا بانشاء class التي ستقم :

-الاتصال ب سرفر google apiو الذي هو بدوره سيعطي جواب على شكل ملف xml .

-ادخال معطيات الملف "tag" داخل جدول .

-إنشاء عدة دوال لسترجاع المعطيات.

--------------------------------------------- GoogleWeatherAPI.php ------------------------------------------

<?php
if(isset($_GET['source'])) {
    highlight_file(__FILE__);
    die;
}
class GoogleWeatherAPI {
	/** اسم المدينة كود**/
	private $city_code = '';
	/** المدينة العترف بها عند google **/
	private $city = '';
	/** اسم الموقع  **/
	private $domain = 'www.google.com';
	/** الصورة **/
	private $prefix_images = '';
	/** جدول الاولي لحالة الطقس **/
	private $current_conditions = array();
	/** الجدول التانوي لحالة الطقسr **/
	private $forecast_conditions = array();
	/** اداكانت المدينة موجودة **/
	private $is_found = true;
	/** The HTML response send by the service **/
	private $response;
	function __construct ($city_code,$lang='fr') {
		$this->city_code = $city_code;
		$this->prefix_images = 'http://'.$this->domain;
		$this->url = 'http://'.$this->domain.'/ig/api?weather='.urlencode($this->city_code).'&hl='.$lang;
		$getContentCode = $this->getContent($this->url);
 		if($getContentCode == 200) {
 			$content = utf8_encode($this->response);
 			$xml = simplexml_load_string($content);
 			if(!isset($xml->weather->problem_cause)) {
 				$xml = simplexml_load_string($content);
 				$this->city = (string)$xml->weather->forecast_information->city->attributes()->data;
 				$this->current_conditions['condition'] = (string)$xml->weather->current_conditions->condition->attributes()->data;
				$this->current_conditions['temp_f'] = (string)$xml->weather->current_conditions->temp_f->attributes()->data;
				$this->current_conditions['temp_c'] = (string)$xml->weather->current_conditions->temp_c->attributes()->data;
				$this->current_conditions['humidity'] = (string)$xml->weather->current_conditions->humidity->attributes()->data;
				$this->current_conditions['icon'] = $this->prefix_images.(string)$xml->weather->current_conditions->icon->attributes()->data;
				$this->current_conditions['wind_condition'] = (string)$xml->weather->current_conditions->wind_condition->attributes()->data;
 				foreach($xml->weather->forecast_conditions as $this->forecast_conditions_value) {
					$this->forecast_conditions_temp = array();
					$this->forecast_conditions_temp['day_of_week'] = (string)$this->forecast_conditions_value->day_of_week->attributes()->data;
					$this->forecast_conditions_temp['low'] = (string)$this->forecast_conditions_value->low->attributes()->data;
					$this->forecast_conditions_temp['high'] = (string)$this->forecast_conditions_value->high->attributes()->data;
					$this->forecast_conditions_temp['icon'] = $this->prefix_images.(string)$this->forecast_conditions_value->icon->attributes()->data;
					$this->forecast_conditions_temp['condition'] = (string)$this->forecast_conditions_value->condition->attributes()->data;
					$this->forecast_conditions []= $this->forecast_conditions_temp;
				}
			} else {
				$this->is_found = false;
			}

		} else {
			trigger_error('Google results parse problem : http error '.$getContentCode,E_USER_WARNING);
			return null;
		}
	}
//استرجاع البيانات html
	public function getContent($url)
    {
		if (!extension_loaded('curl')) {
            throw new Exception('curl extension is not available');
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_URL, $url);
        $this->response = curl_exec($curl);
		$infos = curl_getinfo($curl);
        curl_close ($curl);
        return $infos['http_code'];
    }
	//استرجاع اسم المدينة
	function getCity() {
		return $this->city;
	}
	// استرجاع الجدول الاولي
	function getCurrent() {
		return $this->current_conditions;
	}
	// استرجثع الجدول التانوي
	function getForecast() {
		return $this->forecast_conditions;
	}
	// استرجاع وجود حالة المدينة
	function isFound() {
		return $this->is_found;
	}
}
?>

الان الاستعمال

<?php
include("GoogleWeatherAPI.php");
$gweather = new GoogleWeatherAPI('Fez','fr'); // "en"يمكن استعمال 

if($gweather->isFound()) {
$City=$gweather->getCity();
$dt=$gweather->getCurrent();
}

?>
<div>
Météo à <?php if($gweather->isFound()) { echo $City; } ?>
	</div>
 <center>

	<div class="current">
		<?php if($gweather->isFound()) { echo '<div class="icon" ><img src="'.$dt['icon'].'"/></div>'; } ?>


				<strong><?php if($gweather->isFound()) { echo $dt['temp_c']; } ?> °C</strong><br />
				Actuellement : <?php if($gweather->isFound()) { echo $dt['condition']; } ?><br />
				 <?php if($gweather->isFound()) { echo $dt['wind_condition']; } ?><br />
				<?php if($gweather->isFound()) { echo $dt['humidity']; } ?><br />

 </center>

تم ولله الحمد والشكر

اتمني ان ينال اعجابكم

تم تعديل هذه المشاركة بواسطة billphigor في 26 سبتمبر 2010 في 10:52

مواضيع مشابهة