Giter Site home page Giter Site logo

wsapi-sdk-php's Introduction

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 [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.

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

wsapi-sdk-php's People

Contributors

jeffreydking avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.