Skip to main content

Find Weather Forecast from IP Address

Language


To retrieve the weather forecast for your location based only on your current IP, the full tutorial will have the following 5 sections (http://bit.ly/1A5Y651), 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

Here, I will only show you how to retrieve the weather forecast.

  1. //includes the IP2Location framework
  2. require_once('IP2Location.php');
  3.  
  4. //function that sends and retrieves an answer from a HTTP request
  5. function SendRequest( $url, $method = 'GET', $data = array(), $headers = array('Content-type: application/x-www-form-urlencoded') )
  6. {
  7. $context = stream_context_create(array
  8. (
  9. 'http' => array(
  10. 'method' => $method,
  11. 'header' => $headers,
  12. 'content' => http_build_query( $data )
  13. )
  14. ));
  15.  
  16. return file_get_contents($url, false, $context);
  17. }
  18.  
  19. //the client's IP
  20. $ip = $_SERVER['REMOTE_ADDR'];
  21. // Standard lookup with no cache
  22. $loc = new IP2Location('databases/db24.bin');
  23.  
  24.  
  25. $stationCode=FindIPWeatherStationCode($ip,$loc);
  26. GetWeatherForecastFromYahoo($stationCode);

 

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.