Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Examples of using the WP-DarkSky Helper class: https://github.com/joshuadavidnelson/wp-darksky
<?php
/**
* Basic example of WP-DarkSky useage.
*
* @since 01-04-2017 - Updated for new Dark Sky API & Naming
*
* @link https://github.com/joshuadavidnelson/wp-darksky
* @author Joshua David Nelson, josh@joshuadnelson.com
*/
$args = array(
'api_key' => '', // Enter your API key
'latitude' => '', // enter the longitude
'longitude' => '', // enter the latitude
);
$forecast = new DarkSky\Forecast( $args );
// Get the response, which will be an array similar to this one: https://gist.github.com/joshuadavidnelson/b27ff69e9a99c1500a89
$response = $forecast->get_response();
<?php
/**
* Basic example of WP-DarkSky useage with url query parameters to exclude data blocks.
*
* Remove the ones you don't need for a faster response time. See the darksky.net API documentation for specifics.
*
* @since 01-04-2017 - Updated for new Dark Sky API & Naming
*
* @see https://darksky.net/dev/docs/
*
* @link https://github.com/joshuadavidnelson/wp-darksky
* @author Joshua David Nelson, josh@joshuadnelson.com
*/
$args = array(
'api_key' => '', // Enter your API key
'latitude' => '', // enter the longitude
'longitude' => '', // enter the latitude
'query' => array( 'exclude' => 'flags,currently,minutely,hourly,alerts' ), // Just grab the daily forecast, ignore all others.
);
$forecast = new DarkSky\Forecast( $args );
// Get the response
$response = $forecast->get_response();
<?php
/**
* Basic example of WP-DarkSky useage with a refreshed response.
*
* @since 01-04-2017 - Updated for new Dark Sky API & Naming
*
* @link https://github.com/joshuadavidnelson/wp-darksky
* @author Joshua David Nelson, josh@joshuadnelson.com
*/
$args = array(
'api_key' => '', // Enter your API key
'latitude' => '', // enter the longitude
'longitude' => '', // enter the latitude
);
$forecast = new DarkSky\Forecast( $args );
// Fresh response, clear the cache with the 'true' parameter
$response = $forecast->get_response( true ); // If the parameter is false or not set, it will use the cache.
<?php
/**
* Basic example of WP-DarkSky useage and available caching parameters. Below the defaults are shown.
*
* @since 01-04-2017 - Updated for new Dark Sky API & Naming
*
* @link https://github.com/joshuadavidnelson/wp-darksky
* @author Joshua David Nelson, josh@joshuadnelson.com
*/
$args = array(
'api_key' => '', // Enter your API key
'latitude' => '', // enter the longitude
'longitude' => '', // enter the latitude
'cache_prefix' => 'api_', // careful here, md5 is used on the request url to generate the transient name. You are limited to an 8 character prefix before the combined total exceeds the transient name limit
'cache_enabled' => true, // Enable the cache, or false to skip caching entirely
'cache_time' => 6 * HOUR_IN_SECONDS, // the time in seconds to store the
'clear_cache' => false, // set to true to force the cache to clear
);
$forecast = new DarkSky\Forecast( $args );
// Get the response with default parameters
$response = $forecast->get_response();
// Modify a caching parameter, refresh the response. In this case, we're shortening the cache time.
$forecast->cache_time = 5 * MINUTE_IN_SECONDS;
$response = $forecast->get_response( true );
<?php
/**
* Basic example of WP-DarkSky useage with url query parameters.
*
* @since 01-04-2017 - Updated for new Dark Sky API & Naming
*
* @link https://github.com/joshuadavidnelson/wp-darksky
* @author Joshua David Nelson, josh@joshuadnelson.com
*/
$args = array(
'api_key' => '', // Enter your API key
'latitude' => '', // enter the longitude
'longitude' => '', // enter the latitude
'query' => array( 'units' => 'us', 'exclude' => 'flags' ) // See the Forecast.io API documentation
);
$forecast = new DarkSky\Forecast( $args );
// Get the response
$response = $forecast->get_response();
<?php
/**
* Basic example of WP-DarkSky used to output the current day's min/max temperatures in fahrenheight.
*
* @since 01-04-2017 - Updated for new Dark Sky API & Naming
*
* @link https://github.com/joshuadavidnelson/wp-darksky
* @author Joshua David Nelson, josh@joshuadnelson.com
*/
$args = array(
'api_key' => '', // Enter your API key
'latitude' => '', // enter the longitude
'longitude' => '', // enter the latitude
'query' => array( 'units' => 'us', 'exclude' => 'flags' )
);
$forecast = new DarkSky\Forecast( $args );
// Get the current forecast data for the daily forecast, which provides the next 7 days
$daily = isset( $forecast->daily['data'] ) ? $forecast->daily['data'] : false;
// Pull out the current day's forecast
if( $daily ) {
$date_format = 'n/j/Y'; // format used to compare dates
$time_now = date( $date_format, current_time( 'timestamp' ) );
foreach( $daily as $day ) {
if( isset( $day['time'] ) && $time_now == date( $date_format, $day['time'] ) ) {
echo number_format( $day['temperatureMin'], 0 ) . ' / ' . number_format( $day['temperatureMax'], 0 );
break; // Breaking the foreach here because we found today's forecast, no need to loop through any more
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.