Advertisement
Iv4n

Weather Data Backend

Jan 14th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.92 KB | None | 0 0
  1. <?php
  2. include('forecast.io.php');
  3. $api_key = '_FORECAST_API_KEY_';
  4.  
  5. $latitude = '43.6701';
  6. $longitude = '-79.3866';
  7.  
  8. $forecast = new ForecastIO($api_key);
  9. $condition = $forecast->getCurrentConditions($latitude, $longitude);
  10.  
  11. $currentWeather = 'Temp ' . round($condition->getTemperature()) . '/Feels ' . round($condition->getApparentTemperature()) . '*C';
  12. $response = sendPostData('lcd3',  $currentWeather);
  13. if (strpos($response, '"return_value": 1') === false) echo $response . "\n";
  14. else echo $currentWeather . "\n";
  15.  
  16. $nextHours = $condition->getNextHoursSummary();
  17. $response = sendPostData('lcd4',  $nextHours);
  18. if (strpos($response, '"return_value": 1') === false) echo $response . "\n";
  19. else echo $nextHours . "\n";
  20.  
  21. echo str_repeat('#', 20) . "\n";
  22.  
  23.  
  24. function sendPostData($function, $post) {
  25.   $ch = curl_init('https://api.spark.io/v1/devices/_DEVICE_ID_/' . $function);
  26.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  27.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  28.   curl_setopt($ch, CURLOPT_POST, 1);
  29.   curl_setopt($ch, CURLOPT_POSTFIELDS, 'access_token=_ACCESS_TOKEN_&args='.$post);
  30.   $return = curl_exec($ch);
  31.   echo curl_error($ch);
  32.   curl_close($ch);
  33.   return $return;
  34. }
  35.  
  36. //############################################//
  37. //And this lines in forecast.io.php
  38. //Replaced - @class ForecastIO:
  39.   function getCurrentConditions($latitude, $longitude) {
  40.    
  41.     $data = $this->requestData($latitude, $longitude);
  42.      
  43.     if ($data !== false) {
  44.       $tmpData = $data->currently;
  45.       $tmpData->nextHoursSummary = $data->hourly->summary;
  46.       return new ForecastIOConditions($tmpData);
  47.      
  48.     } else {
  49.      
  50.       return false;
  51.      
  52.     }
  53.  
  54.   }
  55.  
  56. //Added - @class ForecastIOConditions:
  57.   function getApparentTemperature() {
  58.    
  59.     return $this->raw_data->apparentTemperature;
  60.    
  61.   }
  62.  
  63.   function getNextHoursSummary() {
  64.    
  65.     return $this->raw_data->nextHoursSummary;
  66.    
  67.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement