Requires PHP version 5.3.0 or greater.
- Save api.weathersource.sdk directory to your application
- Rename api.weathersource.sdk/sdk/config_sample.php to api.weathersource.sdk/sdk/config.php
- Update api.weathersource.sdk/sdk/config.php with your credentials and preferences
- Include api.weathersource.sdk/sdk/weathersource_api.sdk.php in your application
- Use the SDK at will
To acquire valid credentials, please contact Dave Smith at [email protected]. Eventually, you will be able to sign up for an API account directly at http://weathersource.com, but this is not currently available.
Include the class definition:
include_once('/path/to/api.weathersource.sdk/sdk/weather_source_api_requests.php');
With the proper options set in api.weathersource.sdk/sdk/config.php, this class can:
- Retry requests N times until success
- Log errors to specified directory
- Convert imperial measurement results to metric values
- Convert Fahrenheit results to Celsius values
- Suppress HTTP error codes
- Return diagnostic information with responses
The Weather_Source_API_Requests class (alias: Weather_Source_API_Request) is designed for multithreaded requests. Add multiple requests, and they will be processed as thread capacity allows.
You can optionally define a callback function that will process results as they come available. This will allow processing of results while unprocessed requests continue to complete.
/**
*
* User defined callback function to process results as individual requests complete
*
* @param $result string The response to the cURL response. Prepend with '&' to pass by reference.
* @param $http_code string The HTTP code generated by the cURL request
* @param $latency float Seconds elapsed since node added accurate to the nearest microsecond
* @param $url string User provided URL
* @param $opts array The cURL transfer options
*
* @return NULL
*
**/
function user_defined_callback( &$result, $http_code, $latency, $url, $opts ) {
// do something to $result
}
Make as many requests as you like. Please reference the API documentation at http://developer.weathersource.com for valid request methods, request paths, and request parameters:
$request_1 = new Weather_Source_API_Request(
$request_method = 'GET',
$request_path = 'account',
$request_parameters = array( 'fields' => 'key,username' ),
$callback_name = 'user_defined_callback' // optional
);
$request_2 = new Weather_Source_API_Request(
$request_method = 'GET',
$request_path = 'account',
$request_parameters = array( 'fields' => 'first_name,last_name,email' ),
$callback_name = 'user_defined_callback' // optional
);
Using two static methods, we will now wait for the threaded requests to complete, and grab the results of all requests made:
Weather_Source_API_Requests::finish();
$results = Weather_Source_API_Requests::get_results();
There are also two instance methods at our disposal:
$status_1 = $request_1->get_status(); // returns 'queued', 'processing', 'complete', or 'unknown'
$result_1 = $request_1->get_result(); // returns an array containing the request response if the request status
// is 'complete', FALSE otherwise