Find Weather Forecast from IP Address

In this tutorial, I will present how to retrieve the weather forecast for your location based only on your current IP. The tutorial will have the following 5 sections, that will:

  • Describe elements needed for weather forecast retrieval
  • Describe the weather station code field of IP2Location
  • Show code that gets the weather station code from IP address
  • Show code that retrieves the weather forecast from Yahoo! Weather using the weather station code.
  • Show code that retrieves the weather forecast using IP2Location

Elements needed for retrieving weather location

In order to be able to run the code from this tutorial, you will have to:

Description of weather station field from IP2Location

IP2Location provides information about the closest weather station for your current IP. This information includes the weather station name and the weather station code and it can be found in databases 17,18,20,22 and 24. For this tutorial, you need to download one of these databases. The weather station code is a ID, used in weather.com and is also recognized by https://www.yahoo.com/news/weather/. The format for this ID is: CCXXZZZZ, where:

CC
Two-letter country codes defined in ISO 3166-1. You can find these codes at http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
XX
Two-letter XX
ZZZZ
Four-digit number that identifies in unique mode the weather station in that country.

Examples of these codes are: ROXX0003, FRXX0234.

How to get the weather station code from IP address

In order to get the weather station from the IP address, you will have to write the following lines of code:

function FindIPWeatherStationCode($ip,$locationDatabase)
{
    //get weather information
    $record = $locationDatabase->lookup($ip, IP2Location::ALL);
    echo 'Weather Station Name: ' . $record->weatherStationName;
    echo 'Weather Station Code: ' . $record->weatherStationCode;
    return $record->weatherStationCode;
}

This code searches for the record that has the client’s IP in the database. For one particular record, it prints the weather station name and the weather station code that are the closest to the location of the IP.

How to get the weather forecast from Yahoo! Weather using the weather station code

Using one station code, you can find very easy the forecast of weather, based on Yahoo Web Services. The following code retrieves the weather description for one particular station code.

function GetWeatherForecastFromYahoo($stationCode)
{
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
 
//Format the YQL query
//Example of YQL query: "select * from weather.forecast where u='c' and location='ROXX0003'"
$yql_query = "select * from weather.forecast where u='c' and location='".$stationCode."'";
 
//Format the YQL URL
//Example of YQL URL:   http://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where u%3D'c' and location%3D'ROXX0003'&format=json
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&format=json";
 
//Send and retrieve the answer from Yahoo Web Services
$yql_query_response=SendRequest($yql_query_url);
$yql_query_response_json=json_decode($yql_query_response);
 
//Validate the response
if(is_null($yql_query_response_json->query->results))
{
    echo "No results for weather station ".$record->weatherStationName." (Code:".$record->weatherStationCode.")";
    die ();
}
 
//Retrieve the weather URL page that has information about the weather from that station code
$weather_page=$yql_query_response_json->query->results->channel->link;
echo "Link for weather condition: $weather_page";
 
//This field contains the forecast
echo $yql_query_response_json->query->results->channel->item->description;
 
//You can also redirect the client to Yahoo Web Page
//header("Location: $weather_page");
}

How to get the weather forecast using IP address

The below code that links the above two sections:

//includes the IP2Location framework
require_once('IP2Location.php');
 
//function that sends and retrieves an answer from a HTTP request
function SendRequest( $url, $method = 'GET', $data = array(), $headers = array('Content-type: application/x-www-form-urlencoded') )
{
    $context = stream_context_create(array
    (
        'http' => array(
            'method' => $method,
            'header' => $headers,
            'content' => http_build_query( $data )
        )
    ));
 
    return file_get_contents($url, false, $context);
}
 
//the client's IP
$ip = $_SERVER['REMOTE_ADDR'];
// Standard lookup with no cache
$loc = new IP2Location('databases/db24.bin');
 
 
$stationCode=FindIPWeatherStationCode($ip,$loc);
GetWeatherForecastFromYahoo($stationCode);

In order to be able to run the PHP file, you must have the following files:

  • IP2Location.php in the same folder as the PHP script
  • DB24.bin in a ‘databases’ subfolder

Was this article helpful?

Related Articles