Skip to content

bnaseer/wsapi-sdk-php

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP SDK for the Weather Source API Version 3.0

Requirements

Requires PHP version 5.3.0 or greater.

Installation

  1. Save api.weathersource.sdk directory to your application
  2. Rename api.weathersource.sdk/sdk/config_sample.php to api.weathersource.sdk/sdk/config.php
  3. Update api.weathersource.sdk/sdk/config.php with your credentials and preferences
  4. Include api.weathersource.sdk/sdk/weathersource_api.sdk.php in your application
  5. Use the SDK at will

To acquire valid credentials, please contact Dave Smith at dave@weathersource.com. Eventually, you will be able to sign up for an API account directly at http://weathersource.com, but this is not currently available.

Weather_Source_API_Requests class

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:

  1. Retry requests N times until success
  2. Log errors to specified directory
  3. Convert imperial measurement results to metric values
  4. Convert Fahrenheit results to Celsius values
  5. Suppress HTTP error codes
  6. Return diagnostic information with responses

Use

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

About

The PHP SKD for the Weather Source API

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • PHP 100.0%