从IP地址查找天气预报

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从IP地址查找天气预报相关的知识,希望对你有一定的参考价值。

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. (
  8. 'http' => array(
  9. 'method' => $method,
  10. 'header' => $headers,
  11. 'content' => http_build_query( $data )
  12. )
  13. ));
  14.  
  15. return file_get_contents($url, false, $context);
  16. }
  17.  
  18. //the client's IP
  19. $ip = $_SERVER['REMOTE_ADDR'];
  20. // Standard lookup with no cache
  21. $loc = new IP2Location('databases/db24.bin');
  22.  
  23.  
  24. $stationCode=FindIPWeatherStationCode($ip,$loc);
  25. GetWeatherForecastFromYahoo($stationCode);

以上是关于从IP地址查找天气预报的主要内容,如果未能解决你的问题,请参考以下文章

如何从给定子网中查找第一个和最后一个 IP 地址?

从用户 ip 地址查找位置和区域

最好的网页天气预报代码(自动定制城市天气)

如何查找用户的IP地址

Java如何从IP地址查找主机名?

天气预报代码调用