-1

I am using forecast.io API to deliver weather data to my website. Sometimes the forecast.io API is down, which causes every single page on my website which uses the forecast.io API to return "Service Unavailable, unable to get file content errors"

This is what I'm using to gather the weather data from the API

<?php
include("assets/php/forecast.php");

$api_key = '123123123';

$latitude = $data->getLatitude();
$longitude = $data->getLongitude();

$forecast = new ForecastIO($api_key);

/*
* GET CURRENT CONDITIONS
*/
$condition = $forecast->getCurrentConditions($latitude, $longitude);

$temp = $condition->getTemperature();
$temp = $temp + 2;
$summ = $condition->getSummary();
$icon = $condition->getIcon();
$icon2 = $condition->getIcon();
$icon = str_replace('-', '_', $icon);
$icon2 = str_replace('-', ' ', $icon2);
$icon = strtoupper($icon);
$icon2 = ucfirst($icon2);
?>  

I would like to include some sort of error handler where if the forecast.io API is unavailable, then this code doesn't get called.

seek.estate
  • 111
  • 1
  • 3
  • 11
  • 3
    What does `new ForecastIO()` do when the service is unavailable? What are its error states? Does it throw an exception? – Michael Berkowski May 12 '14 at 14:10
  • 2
    If [this code](https://github.com/tobias-redmann/forecast.io-php-api/blob/master/lib/forecast.io.php) is the source you're using, it makes a `file_get_contents()` call. [This question](http://stackoverflow.com/questions/15620124/http-requests-with-file-get-contents-getting-the-response-code) explains how to inspect the HTTP response code and headers returned by `file_get_contents()`. – Michael Berkowski May 12 '14 at 14:13
  • Note that [per the docs](http://us3.php.net/manual/en/reserved.variables.httpresponseheader.php), `$http_response_header` is created in the local scope, so you might need to modify the ForecastIO class source. – Michael Berkowski May 12 '14 at 14:16
  • I don't understand how to modify the ForecastIO class source – seek.estate May 12 '14 at 14:22
  • https://github.com/tobias-redmann/forecast.io-php-api/blob/master/lib/forecast.io.php yes this is the code include("assets/php/forecast.php"); refers to – seek.estate May 12 '14 at 14:22
  • Now then another clarifying question: Do you want to just add error handling to the ForecastIO class so it throws an appropriate error, or do you want to keep track of that in your user's session so that subsequent pages make no attempt to contact ForecastIO at all? – Michael Berkowski May 12 '14 at 14:26
  • I would like it to keep track of that my user's session so that subsequent pages make no attempt to contact ForecastIO at all – seek.estate May 13 '14 at 06:35

1 Answers1

0

This is the site where I had the problem https://seek.estate/

https://seek.estate/buy/422-melbournes-most-liveable-home-sea-views-elevator-designer-masterpiece

This is what I changed

$forecast = new ForecastIO($api_key);
$request_url = 'https://api.forecast.io/forecast/';

$content = file_get_contents($request_url);

if (!empty($content)) {

  return condition = $forecast->getCurrentConditions($latitude, $longitude);

} else {

  return false;

}
seek.estate
  • 111
  • 1
  • 3
  • 11