I am trying to create a snippet that will take in a Dark Sky API key, latitude, longitude, number of hours to be forecasted, and return an hourly forecast formatted by a chunk. I am completely new to php, and programming in general, so please bear with me as I cobbled this together from a youtube tutorial
I would like the content to populate the tags [[+hour]] [[+temp]] [[+humidity]] [[+windspeed]] [[+summary]] with the proper information from the returned JSON. The hour using the date() function to format properly. I can loop through it, but I am completely lost in how to proceed from there, and confused by documentation.
$coordinates = $lat.','.$lng
$api_url = 'https://api.darksky.net/forecast/'.$api.'/'.$coordinates;
$forecast = json_decode(file_get_contents($api_url));
date_default_timezone_set($forecast->timezone);
$i = 0;
foreach($forecast->hourly->data as $hour) {
// Originally just an echo call, unsure how to proceed to work with chunk.
$temp = round($hour->temperature)
$i++;
if($i==$hours) break;
}
So something like this? This doesnāt seem complete, but this is what I got thus far. How does this take into account the tpl parameter passed to the chunk?
Ok I replaced it with the getChunk and removed the .weather in the placeholder names, but no output as of yet. What is the last step?
<?php
$coordinates = $lat.','.$lng;
$api_url = 'https://api.darksky.net/forecast/'.$api.'/'.$coordinates;
$forecast = json_decode(file_get_contents($api_url));
//Set timezone based on location searched
date_default_timezone_set($forecast->timezone);
//Set counter at zero
$i = 0;
// Start the foreach loop to get hourly forecast
foreach($forecast->hourly->data as $hour) {
$modx->getChunk($tpl,array(
'hour' => date("g a",$hour->time),
'temp' => round($hour->temperature),
'humidity' => $hour->humidity*100,
'windspeed' => round($hour->windSpeed)
));
// Increase counter by one for each iteration
$i++;
//Stop after set number of iterations
if($i==$hours) break;
}
No errors when using the getChunk() function with no prefixes on the tag names. Same goes for using the setPlaceholders() function Mr_JimWest used, but I think that was for another use. getChunk() seems the way to go.
@Mr_JimWest Yes I think you misunderstood me. I donāt want it to fill in tags in the page, I want it to loop through records, and outputting them using a chunk with placeholders. Similar to how getResources works, but calling information from Dark Sky API JSON.
So just to clarify, since this thread got a bit jumbled, I should have been more descriptive. Here is what I have, but get no output at all, not even the html in the chunk, and no errors in the error log. I just get nothing.
Snippet:
<?php
$coordinates = $lat.','.$lng;
$api_url = 'https://api.darksky.net/forecast/'.$api.'/'.$coordinates;
$forecast = json_decode(file_get_contents($api_url));
//Set timezone based on location searched
date_default_timezone_set($forecast->timezone);
//Set counter at zero
$i = 0;
// Start the foreach loop to get hourly forecast
foreach($forecast->hourly->data as $hour) {
$modx->getChunk($tpl,array(
'hour' => date("g a",$hour->time),
'temp' => round($hour->temperature),
'humidity' => $hour->humidity*100,
'windspeed' => round($hour->windSpeed)
));
// Increase counter by one for each iteration
$i++;
//Stop after set number of iterations
if($i==$hours) break;
}
Snippet call in page template, called uncached for testing: (api key will change once this thread closes).
As a quick fix for the error messages, you could add some validation to make sure the variables have values before running the snippet. For example:
<?php
// Early return if there are no values.
if(!$api || !$coordinates) return '';
$output = [];
$coordinates = $lat . ',' . $lng;
$api_url = 'https://api.darksky.net/forecast/' . $api . '/' . $coordinates;
$forecast = json_decode(file_get_contents($api_url));
//Set timezone based on location searched
date_default_timezone_set($forecast->timezone);
//Set counter at zero
$i = 0;
// Start the foreach loop to get hourly forecast
foreach ($forecast->hourly->data as $hour) {
$output[] = $modx->getChunk($tpl, [
'hour' => date('g a', $hour->time),
'temp' => round($hour->temperature),
'humidity' => $hour->humidity * 100,
'windspeed' => round($hour->windSpeed)
]);
// Increase counter by one for each iteration
$i++;
//Stop after set number of iterations
if ($i == $hours) {
break;
}
}
return implode("\n", $output);
But thatās not solving the underlying issue as to why itās happening in the first place.
Is your error_page system setting set to the same resource youāre running the snippet on by any chance?