وبلاگ کدنویسان

آموزش برنامه نویسی

وبلاگ کدنویسان

آموزش برنامه نویسی

حتما دیده اید که سایت های اینترنتی، اپلیکیشن های موبایل یا ربات های تلگرام، آب و هوای شهرها را نمایش می دهند . در این قسمت قطعه کدی با PHP برای دریافت مشخصات آب و هوایی شهر و نمایش آن خواهیم نوشت.

در آدرس https://developer.yahoo.com/weather/#get-started از یاهو می توانید راهنمای استفاده از api یاهو برایدریافت وضعیت آب و هوا را مشاهده کنید. در همین آدرس ، نحوه درخواست وضعیت آب و هوا برای شهر مورد نظر را آموزش داده و مثال هایی برای دریافت اطلاعات مختلف نشان داده است .

به عنوان مثال گفته شده برای دریافت وضعیت آب و هوای شهر Nome در AK درخواست زیر را بفرستید:


https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

و من برای دریافت وضعیت آب و هوایی شهر بیرجند در خراسان جنوبی درخواست زیر فرستادم و جواب گرفتم، شما هم می تونید رو لینک زیر کلیک کنید تا نتیجه اون مشاهده کنید.

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22birjand%2C%20ir%22)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

پارامتر format  مشخص می کنه که پاسخ سرور یاهو به این درخواست به چه صورتی باشه، اگر format=json قرار بدین، پاسخ دارایقالب json  اگر قرار بدین format=xml ، پاسخ دارای XML خواهد بود . اگر فرمت جی سان رو استفاده کنید میتونید با کمک تابع json_decode از پی اچ پی پاسخ رو رمزگشایی کنید و اطلاعات مورد نیازتون رو  بدست بیارید. نمونه ای از اطلاعات جی سان برای درخواست زیر  نشان داده شده است:

{"query":{"count":1,"created":"2017-10-27T20:35:16Z","lang":"en-us","results":{"channel":{"units":{"distance":"mi","pressure":"in","speed":"mph","temperature":"F"},"title":"Yahoo! Weather - Birjand, South Khorasan, IR","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2254447/","description":"Yahoo! Weather for Birjand, South Khorasan, IR","language":"en-us","lastBuildDate":"Sat, 28 Oct 2017 12:05 AM IRST","ttl":"60","location":{"city":"Birjand","country":"Iran","region":" South Khorasan"},"wind":{"chill":"63","direction":"113","speed":"4"},"atmosphere":{"humidity":"21","pressure":"838.0","rising":"0","visibility":"16.1"},"astronomy":{"sunrise":"5:48 am","sunset":"4:45 pm"},"image":{"title":"Yahoo! Weather","width":"142","height":"18","link":"http://weather.yahoo.com","url":"http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"},"item":{"title":"Conditions for Birjand, South Khorasan, IR at 10:30 PM IRST","lat":"32.883308","long":"59.21162","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2254447/","pubDate":"Fri, 27 Oct 2017 10:30 PM IRST","condition":{"code":"29","date":"Fri, 27 Oct 2017 10:30 PM IRST","temp":"63","text":"Partly Cloudy"},"forecast":[{"code":"32","date":"28 Oct 2017","day":"Sat","high":"78","low":"57","text":"Sunny"},{"code":"32","date":"29 Oct 2017","day":"Sun","high":"79","low":"58","text":"Sunny"},{"code":"32","date":"30 Oct 2017","day":"Mon","high":"79","low":"57","text":"Sunny"},{"code":"32","date":"31 Oct 2017","day":"Tue","high":"76","low":"56","text":"Sunny"},{"code":"32","date":"01 Nov 2017","day":"Wed","high":"76","low":"56","text":"Sunny"},{"code":"32","date":"02 Nov 2017","day":"Thu","high":"74","low":"57","text":"Sunny"},{"code":"30","date":"03 Nov 2017","day":"Fri","high":"74","low":"56","text":"Partly Cloudy"},{"code":"28","date":"04 Nov 2017","day":"Sat","high":"75","low":"57","text":"Mostly Cloudy"},{"code":"28","date":"05 Nov 2017","day":"Sun","high":"73","low":"56","text":"Mostly Cloudy"},{"code":"32","date":"06 Nov 2017","day":"Mon","high":"71","low":"55","text":"Sunny"}],"description":"<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/29.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Partly Cloudy\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Sat - Sunny. High: 78Low: 57\n<BR /> Sun - Sunny. High: 79Low: 58\n<BR /> Mon - Sunny. High: 79Low: 57\n<BR /> Tue - Sunny. High: 76Low: 56\n<BR /> Wed - Sunny. High: 76Low: 56\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2254447/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n<BR />\n]]>","guid":{"isPermaLink":"false"}}}}}}

نمونه کد زیر با زبان php ابتدا درخواست را برای شهر بیرجند به یاهو فرستاده و پاسخ را با فرمت جی سان دریافت کرده و فیلد description آنرا نشان می دهد. این قطعه کدمی تواند در هر زمینه ای از جمله ربات نویسی تلگرام یا وب سایت های اینترنتی استفاده شود:

<!doctype html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document
</title>
</head>
<body>
<?php $url="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22birjand%2C%20ir%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$data = file_get_contents($url);
$arr = json_decode($data);
echo $arr->query->results->channel->item->description; ?>
</body>
</html>

با اجرای این مثال خروجی زیر نشان داده می شود که خلاصه ای از وضعیت آب و هوای شهر بیرجند در ایران است.


  • محمد محمدی

نظرات  (۰)

هیچ نظری هنوز ثبت نشده است
ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی