Yahoo Weather API PHP Script Example/Widget

Yahoo Weather API PHP Script Example/Widget  

In this example, I will show how to create weather application using Yahoo Weather API.Yahoo Weather API allows developers and programmers to get up-to-date weather information for your location.Yahoo Weather API provides local & long range Weather Forecast, Current Conditions, weather icons and two day future.

<?php
/*

Example: Yahoo! Weather API Example Using SimpleXML in PHP.

The base URL for the Weather RSS feed is
http://weather.yahooapis.com/forecastrss

Parameters
w WOEID 
e.g: w=111111

u Units for temperature
f: Fahrenheit
c: Celsius 
e.g.: u=c

*/


$city="bangalore";

$result1 = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22$city%22&format=xml");
$xml1 = simplexml_load_string($result1);
 $woeid=$xml1->results->place->woeid;

if($woeid!="")
{
$fetchData = file_get_contents("http://weather.yahooapis.com/forecastrss?w=$woeid&u=c");
$xmlData = simplexml_load_string($fetchData);
$location = $xmlData->channel->xpath('yweather:location');


if(!empty($location))
{

foreach($xmlData->channel->item as $data)
{
$current_condition = $data->xpath('yweather:condition');
$forecast = $data->xpath('yweather:forecast');
$current_condition = $current_condition[0];
echo "
<table width=40% border=1 align=center>
<tr>
<td align=center style='background-color:yellow'>
  <h1>{$location[0]['city']}, {$location[0]['region']}</h1>
<small>Date: {$current_condition['date']}</small>
<h2>Current Temprature</h2>
<p>
<span style=\"font-size:64px;font-color:red; font-weight:bold;\">{$current_condition['temp']}°C</span>
<br/>
<img src=\"http://l.yimg.com/a/i/us/we/52/{$current_condition['code']}.gif\" style=\"vertical-align: middle;\"/>
{$current_condition['text']}
</p>
<h2>Forecast</h2>
<b>{$forecast[0]['day']}</b> : {$forecast[0]['text']}. <b>High:</b> {$forecast[0]['high']} <b>Low:</b> {$forecast[0]['low']}
<br/>
<b>{$forecast[1]['day']}</b> - {$forecast[1]['text']}. <b>High:</b> {$forecast[1]['high']} <b>Low:</b> {$forecast[1]['low']}
</p>
</td>
</tr></table>
";
}
}
else
{
echo "<h1>please try a different City.</h1>";
}


}
else
{
echo "<h1>Please try a different City.</h1>";
}

?>



OUTPUT


I hope above example will help you to get weather Information for your location.
For more details check : http://developer.yahoo.com/weather


Bookmark and Share

Create Dynamic Bar Graph/Chart in php

Create Dynamic Bar Graph/Chart in php

In this article, I will show how to dynamically create bar graph in PHP. I have created PHP script to create a bar graph in php such that x-scale and y-scale changes dynamically.

Example : Create dynamic Graph/Chart image page using MySQL&PHP

<?php

$hrs= array( "0" => 11, "1" => 12 ,"2" => 13 ,"3" => 14 ,"4" => 15);

$data= array ("0" => 15 ,"1" => 11, "2" => 5, "3" => 15 ,"4" => 17 );

$hrs_len=sizeof($hrs);

if(!empty($data) && !empty($hrs))
{
$data=array_combine($hrs,$data);

$image_width=($hrs_len+1)*32;
$image_height=80;
$padding=20;


$graph_width=$image_width - $padding * 2;
$graph_height=$image_height - $padding * 2;
$image=imagecreate($image_width,$image_height);
$bar_width=25;
$total_bars=count($data);
$gap= 5;
$background_color=imagecolorallocate($image,255,255,255);
$border_color=imagecolorallocate($image,255,255,255);
$line_color=imagecolorallocate($image,255,255,255);


$max_value=max($data);
$ratio= $graph_height/$max_value;
$horizontal_data=20;
$horizontal_gap=$graph_height/$horizontal_data;
for($i=1;$i<=$horizontal_data;$i++){
$y=$image_height - $padding - $horizontal_gap * $i ;
imageline($image,$padding,$y,$image_width-$padding,$y,$line_color);
$v=intval($horizontal_gap * $i /$ratio);
}
   $bar_color=imagecolorallocate($image,50,205,50);
for($i=0;$i< $total_bars; $i++){
list($key,$value)=each($data);
$x1= $padding + $gap + $i * ($gap+$bar_width) ;
$x2= $x1 + $bar_width;
$y1=$padding +$graph_height- intval($value * $ratio) ;
$y2=$image_height-$padding;

imagestring($image,0,$x1+3,$y1-10,$value,$bar_color);
imagestring($image,0,$x1+3,$image_height-15,$key,$bar_color);
imagefilledrectangle($image,$x1,$y1,$x2,$y2,$bar_color);
}
header("Content-type:image/png");
imagepng($image);
}
?>


I hope this example will help you to create Dynamic Bar Graph/Chart in php.


Bookmark and Share

HTML5 interview questions and Answers - HTML5 Quiz.

HTML5 interview questions and Answers - HTML5 Quiz.

In this section, I have selected advanced HTML5 interview questions and answers for Fresher and experienced cadidates. Last few months I have been working to select best HTML5 Job interview question and answer set for IT interview questions.

HTML5 interview questions and Answers:


What is HTML5?
Ans :  HTML5 is The New HTML Standard with new elements, attributes, and behaviors.

----------------------------------------------------------------------------------------------
HTML 5 Features:
Ans :
  1.  The <canvas> element for 2D drawing
  2. The <video> and <audio> elements for media playback
  3. local storage support.
  4. Added New elements, like <figure>,<small>, <header>, <nav>,<article>, <footer>, <section>,<mark>
  5. New form controls, like placeholder,calendar, date, time, email, url, search,required ,autofocus
  6. In HTML5 there is only one <!doctype> declaration: <!DOCTYPE html>
----------------------------------------------------------------------------------------------
What is HTML5 Web Storage?
Ans : In HTML5, we can store data locally within the user's browser.It is possible to store large amounts of data without affecting the website’s performance.
Web Storage is more secure and faster.
there are two types of Web Storages
1.LocalStorage:stores data locally with no limit
2.SessionStorage:stores data for one session

----------------------------------------------------------------------------------------------
How to store data on client in HTML5?
Ans : we can store data using HTML5 Web Storage.

1.LocalStorage 

<script type="text/javascript">
localStorage.name="Raj";
document.write(localStorage.name);
</script>

2.SessionStorage
<script type="text/javascript">
sessionStorage.email="test@gmail.com";
document.write(sessionStorage.email);
</script>

----------------------------------------------------------------------------------------------
How do you play a Video using HTML5?

Ans : HTML5 defines a new element to embed a video on Web Page
the <video> element.

Example:
<video width="500" height="300" controls>
  <source src="video1.mp4" type="video/mp4">
</video>

----------------------------------------------------------------------------------------------
How do you play a Audio using HTML5?

Ans : HTML5 defines a new element to embed a video on Web Page
the <audio> element.

Example:
<audio controls>
  <source src="audio.mp3" type="audio/mpeg">
</audio>

----------------------------------------------------------------------------------------------
Canvas Element in HTML5?
Ans : The canvas element is used to draw graphics images on a web page

<canvas id="canvas_image" width="400" height="200"></canvas>

The canvas is a two-dimensional grid.

----------------------------------------------------------------------------------------------
HTML5 <input> Types ?
Ans : 
  • search
  • tel
  • time
  • color
  • email
  • month
  • date
  • datetime
  • datetime-local
  • number
  • range
  • url
  • week


----------------------------------------------------------------------------------------------
HTML5 New Form Attributes?
Ans :
  • pattern
  • placeholder
  • required
  • step
  • autocomplete
  • autofocus
  • height and width
  • list
  • min and max
  • multiple
  • form
  • formaction
  • formenctype
  • formmethod
  • formnovalidate
  • formtarget
----------------------------------------------------------------------------------------------
What does a <hgroup> tag do?

Ans : The <hgroup> tag is used to group heading elements.
The <hgroup> element is used to group a set of <h1> to <h6> elements.

<hgroup>
<h1>Hello</h1>
<h2>How r u?</h2>
</hgroup>

----------------------------------------------------------------------------------------------
Which video formats are used for the video element?
Ans : 
Internet Explorer 9+: MP4
Chrome 6+: MP4, WebM, Ogg
Firefox 3.6+ : WebM, Ogg
Safari 5+ : MP4,
Opera 10.6+ : WebM,Ogg

----------------------------------------------------------------------------------------------
Difference between HTML4 and HTML5 
----------------------------------------------------------------------------------------------
What is the <!DOCTYPE> ? Is it necessary to use in HTML5 ? 
Ans : The <!DOCTYPE> declaration must be the very first thing in HTML5 document, before the <html> tag

----------------------------------------------------------------------------------------------
What are the New Media Elements in HTML5?
Ans : 
  • <audio>
  • <video>
  • <source>
  • <embed>
  • <track>


Bookmark and Share

Post data to a specific port using PHP/Curl.

Post data to a specific port using PHP/Curl.

In this article,I will show how to Post data to a specific port using PHP/Curl Method. PHP-curl library can be used to communicate with a specific port under Linux and windows platform.

Example: Send data to Port in PHP.

  <?php
ini_set("display_errors",1);
error_reporting(E_ERROR);

$msg="&command=1";
$url = "http://127.0.0.1:1234";

$curl = curl_init();
curl_setopt($curl,  CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $msg);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);

if($response!="")
{
$fp = fopen("test.txt","w");
fwrite( $fp, $response);
fclose($fp);
}

curl_close($curl);
?>





Bookmark and Share

Apache Interview Questions and Answers - Apache Web Server

Apache Interview Questions and Answers - Apache Web Server


In this section, I have selected Basic and Important Apache interview questions and answers for all levels of candidates(Fresher level, experienced level).Last few months I have been working to select best question and answer set for Apache  interview questions.

Apache Interview Questions and Answers:
------------------------------------------------------------------------------------------------------------

How do you set up a virtual host in Apache?

Ans: <VirtualHost www.test.com>
    ServerAdmin admin@test.com
    DocumentRoot /home/Public_html/site
    ServerName www.test.com
    ErrorLog /home/apache/logs/error/site/error_log
    TransferLog /home/apache/logs/access/site/access_log
</VirtualHost>
------------------------------------------------------------------------------------------------------------

htpasswd :
Ans: It creates a new user and asks to specify a password for that user.
------------------------------------------------------------------------------------------------------------

What's the command to stop Apache?
Ans:
apachectl -k stop
/etc/init.d/apache2 stop (Linux)
------------------------------------------------------------------------------------------------------------

What's the command to Restart Apache?
Ans:
apachectl -k restart
/etc/init.d/apache2 restart
------------------------------------------------------------------------------------------------------------

What's the location of log files for Apache server ?
Ans:  /var/log/httpd
------------------------------------------------------------------------------------------------------------

What's the Comamnd to check the version of Apache server ?
Ans: rpm -qa |grep httpd
------------------------------------------------------------------------------------------------------------

Apache server works on which ports ?
Ans: http – port 80
     https – port 443
------------------------------------------------------------------------------------------------------------

How t to enable PHP scripts on your server?
Ans: If you have mod_php installed, use AddHandler.
AddHandler application/x-httpd-php .phtml .php
------------------------------------------------------------------------------------------------------------

If you specify both deny from all and allow from all, what will be the default action of Apache?
Ans: Deny always takes precedence over allow.
------------------------------------------------------------------------------------------------------------

How do you change the default web root?
Ans: Change the DocumentRoot in httpd.conf file.
------------------------------------------------------------------------------------------------------------

How to enable htaccess on Apache?
Ans:
Open httpd.conf and remove the comment on line from

;LoadModule rewrite_module modules/mod_rewrite.so

Find the following
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

and change it to
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
------------------------------------------------------------------------------------------------------------

Does Apache act as Proxy server?
Ans: Yes,using mod_proxy module.
------------------------------------------------------------------------------------------------------------


Bookmark and Share