Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Weather API Call not producing data or completing function


Go to End


6 Posts   630 Views

Avatar
Piixel Designs

Community Member, 8 Posts

17 March 2015 at 7:14pm

Hi All,

I have an API which I am using to show live weather on my website, however I am trying to run the function on my Page.php file as it will be used on every page throughout the site. Please find my function below, it seems to not make it past the if(asset) line. I have been throwing in extra returns to try and see some result only which I see is "Data =." on my website.

Any help would be great.

Thanks in advance.

public function WeatherData() {
$forecast = new ForecastIO();
$data = @$forecast->getForecastToday(-34.051397, 151.154069);

return "Data = " . $data;
return $forecast;

if (isset($data) && is_object($data)) {

$weather = array(
'Temperature' => round($data->getTemperature()) . '°',
'Summary' => $data->getSummary(),
'Icon' => $data->getIcon(),
'WindSpeed' => round(($data->getWindSpeed() / 1.852)),
'Humidity' => ($data->getHumidity() * 100) . '%',
'CloudCover' => ($data->getCloudCover() * 100) . '%'
);

if ($data->getWindBearing()) {
$directions = array('N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N');
$weather['WindBearing'] = $directions[round($data->getWindBearing()/45)];
}

$todayData = @$forecast->getForecastToday(-34.051397, 151.154069);

return new ArrayData($weather);
return "true";
} else {
return "false";
return false;
}

}

Avatar
Devlin

Community Member, 344 Posts

17 March 2015 at 10:28pm

I see is "Data =." on my website

@$forecast->getForecastToday() is not returning anything.

Please remove the '@' sign in $data = @$forecast->getForecastToday() -- error suppression is really bad practice -- and put the site in dev mode to get proper error messages.

Avatar
Piixel Designs

Community Member, 8 Posts

18 March 2015 at 10:07pm

OK I did remove the @ symbol and there was an error validating the SSL Certificate, I have used CURL to access the URL in place of file_get_contents.
This is now running without any errors however I still can't see any data/content coming through only what I mentioned in my first post.

Avatar
Devlin

Community Member, 344 Posts

18 March 2015 at 10:36pm

Edited: 18/03/2015 10:41pm

Well. One step closer to home.

If you'd do a var_dump($data), what would be its output.

Avatar
Piixel Designs

Community Member, 8 Posts

19 March 2015 at 9:10pm

Edited: 19/03/2015 9:10pm

I receive just a "NULL" when doing the var_dump($data);

Also on the url that the content is coming from does have content and I am running json_decode.

Avatar
Devlin

Community Member, 344 Posts

19 March 2015 at 9:36pm

Edited: 19/03/2015 10:16pm

Seems like getForecastToday() does not return any data and your function will stop by "if (isset($data) && is_object($data))", because it's neither isset() nor a object. You will have to investigate getForecastToday() and this is not a SilverStripe issue.

Also on the url that the content is coming from does have content and I am running json_decode.

Well json_decode happen to return NULL on an error. Maybe a json_last_error() will shed some light in this matter.

http://php.net/manual/de/function.json-last-error.php