*/
/* CODE SAMPLE: GETTING WEATHER OBSERVATIONS FOR A FIELD LOCATION */
// Include Header
// Be sure to change the variables in this header.php file, especially adding your
// API Key and Secret or else the API calls will not run. This file uses three helper
// functions--GetOAuthToken(), makeAPICall(), and parseHTTPHeaders()--to streamline
// basic API operations.
include "header.php";
// GET A TOKEN
// First, you always need to generate a token. We built the GetOAuthToken
// function (in header.php) to streamline that part
echo "<h1>Get Access Token</h1>";
try {
    //if there is a cURL problem and the API call can't execute at all,
    //the function throws an exception which we can catch to fail gracefully.
    $access_token = GetOAuthToken($api_key, $api_secret);
} catch (Exception $e) {
    echo $e->getMessage();
    // For this script we're just echoing the error and stopping the rest of the script.
    exit;
    // in your code you'll want to handle the error and recover appropriately.
}
echo "<p>Access Token = {$access_token}</p>";
// MAKE API CALL
// The next example gets the recent weather observations for the field location that you just created.
// Note we're creating the URL first, using the variables from the header.php file.
echo "<hr><h1>Get Recent Weather Observations</h1>";
$observedWeatherURL = 'https://api.awhere.com/v2/weather/fields/' . $new_field_id . '/observations/' . $observed_weather_start . ',' . $observed_weather_end;
try {
    $observedWeatherResponse = makeAPICall('GET', $observedWeatherURL, $access_token, $observedWeatherStatusCode, $observedWeatherResponseHeaders);
} catch (Exception $e) {
Пример #2
0
<?php

// Add your consumer key and secret here
$consumer_key = "";
$consumer_secret = "";
// Constants
$host = "https://api.awhere.com";
$oauth_token = GetOAuthToken();
// Usage Samples
$queryStringUrl = BuildUrlWithExampleQueryString();
print "Sample GET Response (Celsius):\n";
print_r(GetWeatherJson($queryStringUrl));
$queryStringUrl = BuildUrlWithExampleQueryString(true);
print "Sample GET Response (Fahrenheit):\n";
print_r(GetWeatherJson($queryStringUrl));
// Sample GET request to the Weather API, using the PHP cURL extension
//
// Returns an array of WeatherResponse objects either in json or PHP object form
function GetWeatherJson($queryStringUrl, $json = true)
{
    global $oauth_token;
    $ch = curl_init($queryStringUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json", "Authorization: Bearer {$oauth_token}"));
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    return $json ? CurlExecute($ch) : json_decode(CurlExecute($ch));
}
// A utility function to encapsulate curl error handling
function CurlExecute($ch)
{