Last active
January 5, 2017 05:15
-
-
Save joshuadavidnelson/d6fa0c17faf3f0ea0192 to your computer and use it in GitHub Desktop.
Examples of using the WP-DarkSky Helper class: https://github.com/joshuadavidnelson/wp-darksky
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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