Hi, I have made a script:
<?php
define('QUERY_0', 'http://api.hostip.info/get_html.php?ip=');
define('QUERY_1', 'http://www.google.com/ig/api?weather=');
function getWeather() {
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
$url0 = QUERY_0.$ip;
$ch0 = curl_init();
curl_setopt($ch0, CURLOPT_URL, $url0);
curl_setopt($ch0, CURLOPT_HEADER, 0);
curl_setopt($ch0, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch0, CURLOPT_FAILONERROR, TRUE);
$geo = curl_exec($ch0);
curl_close($ch0);
if( preg_match('/City: (.*)\nIP:/', $geo, $matches) ){
$results = $matches[1];
}else{
$results = 'getLocation failure';
}
$location = urlencode($results);
$url1 = QUERY_1.$location;
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url1);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch1, CURLOPT_FAILONERROR, TRUE);
$weather = curl_exec($ch1);
curl_close($ch1);
$xml = new SimplexmlElement($weather);
echo '<link type="text/css" rel="stylesheet" href="weather.css">';
echo '<div id="weather">';
foreach($xml->weather as $item) {
foreach($item->forecast_information as $new) {
echo '<div class="weatherIcon">';
echo '<center>';
echo '<h4>';
echo $new->city['data'];
echo '</h4>';
echo '</center>';
echo '</div>';
}
foreach($item->current_conditions as $new) {
echo '<div class="weatherIcon">';
echo '<center>';
echo '<img src="http://www.google.com/' .$new->icon['data'] . '"/><br/>';
echo '<h5>';
echo $new->condition['data'];
echo '</h5>';
echo '<h6>';
echo $new->temp_c['data'];
echo '</h6>';
echo '<h6>';
echo $new->humidity['data'];
echo '</h6>';
echo '<h6>';
echo $new->wind_condition['data'];
echo '</h6>';
echo '</center>';
echo '</div>';
}
}
echo '</div>';
}
getWeather();
?>
I wish to make this script into a function for my Wordpress theme that can be placed anywhere on my site. I'm new to PHP and all my efforts thus far have failed, any help would be greatly appreciated! 🙂