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:

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 and is also recognized by The format for this ID is: CCXXZZZZ, where:
Two-letter country codes defined in ISO 3166-1. You can find these codes at
Two-letter XX
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 = "";

//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: * 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

//Validate the response
    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
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

//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
// Standard lookup with no cache
$loc = new IP2Location('databases/db24.bin');


In order to be able to run the PHP file, you must have the following files:
Do you like this article? Share it with others by clicking the social media buttons below. We will write more articles related to this topic.