Skip to content

Instantly share code, notes, and snippets.

@psdtohtml5
Last active June 5, 2020 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psdtohtml5/6114534 to your computer and use it in GitHub Desktop.
Save psdtohtml5/6114534 to your computer and use it in GitHub Desktop.
PHP : Weather Widget
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
//check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
//to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$ip = getRealIpAddr();
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
$location = $details->city;
$yahooResult = json_decode(file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20name%2Cwoeid%20from%20geo.places%20where%20text%3D%22" . $location . "%22%20limit%201&format=json"));
$woeid = $yahooResult->query->results->place->woeid;
$weatherDetails = json_decode(file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20units%2C%20item%20from%20weather.forecast%20where%20woeid%3D" . $woeid . "&format=json"));
echo $weatherTitle = $weatherDetails->query->results->channel->item->title;
echo $weatherDate = $weatherDetails->query->results->channel->item->condition->date;
echo $weatherTemp = $weatherDetails->query->results->channel->item->condition->temp . " " . $weatherDetails->query->results->channel->units->temperature;
echo $weatherCond = $weatherDetails->query->results->channel->item->condition->text;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment