} catch (Exception $e) { echo $e->getMessage(); exit; } if ($forecastStatusCode == 200) { // Code 200 means the request was successful echo '<p>You requested ' . count($forecastResponse->forecasts) . " days of forecast." . "The forecasted weather on " . date("F j, Y", strtotime($forecastResponse->forecasts[0]->date)) . " is a high temperature of " . $forecastResponse->forecasts[0]->forecast[0]->temperatures->max . "°" . $forecastResponse->forecasts[0]->forecast[0]->temperatures->units . " and a low of " . $forecastResponse->forecasts[0]->forecast[0]->temperatures->min . "°" . $forecastResponse->forecasts[0]->forecast[0]->temperatures->units . "</p>"; echo '<p>Request:</p><pre>GET ' . $forecastURL . '</pre>'; echo '<p>Content-Range Header:</p>'; // HTTP transactions return a lot of headers, but in this example we only want the Content-Range header // (the parseHTTPHeaders function returns just the headers you want) // This API returns a ranged result, which are paginated by default to 50 results per page. The // Content-Range header shows which of the results are on this page (e.g., 1-10) and the total number // of results. It looks something like this: // Content-Range: observations 0-5/5 echo "<pre>" . parseHTTPHeaders($forecastResponseHeaders, array('Content-Range')) . "</pre>"; echo '<p>Response Body:</p>'; echo '<pre>'; echo stripslashes(json_encode($forecastResponse, JSON_PRETTY_PRINT)); //Note: Stripslashes() is used just for prettier echo '</pre>'; //output in the browser. Not needed normally. } else { // If there is any other response code, there was a problem. // this code shows how to extract the two different error messages // You should not use the error messages themselves to drive behavior // (don't test them in if() or switch() statements) // use the status code for that. See developer.awhere.com/api/conventions echo "<p>ERROR: " . $forecastStatusCode . " - " . $forecastResponse->simpleMessage . "<br>"; echo $forecastResponse->detailedMessage . "</p>"; }
} catch (Exception $e) { echo $e->getMessage(); exit; } if ($observedWeatherStatusCode == 200) { // Code 200 means the request was successful echo '<p>You requested ' . count($observedWeatherResponse->observations) . " days of historical " . "observed weather. The weather on " . date("F j, Y", strtotime($observedWeatherResponse->observations[0]->date)) . " was a high temperature of " . $observedWeatherResponse->observations[0]->temperatures->max . "°" . $observedWeatherResponse->observations[0]->temperatures->units . " and a low of " . $observedWeatherResponse->observations[0]->temperatures->min . "°" . $observedWeatherResponse->observations[0]->temperatures->units . "</p>"; echo '<p>Request:</p><pre>GET ' . $observedWeatherURL . '</pre>'; echo '<p>Content-Range Header:</p>'; // HTTP transactions return a lot of headers, but in this example we only want the Content-Range header // (the parseHTTPHeaders function returns just the headers you want) // This API returns a ranged result, which are paginated by default to 50 results per page. The // Content-Range header shows which of the results are on this page (e.g., 1-10) and the total number // of results. It looks something like this: // Content-Range: observations 0-5/5 echo "<pre>" . parseHTTPHeaders($observedWeatherResponseHeaders, array('Content-Range')) . "</pre>"; echo '<p>Response Body:</p>'; echo '<pre>'; echo stripslashes(json_encode($observedWeatherResponse, JSON_PRETTY_PRINT)); //Note: Stripslashes() is used just for prettier echo '</pre>'; //output in the browser. Not needed normally. } else { // If there is any other response code, there was a problem. // this code shows how to extract the two different error messages // You should not use the error messages themselves to drive behavior // (don't test them in if() or switch() statements) // use the status code for that. See developer.awhere.com/api/conventions echo "<p>ERROR: " . $observedWeatherStatusCode . " - " . $observedWeatherResponse->simpleMessage . "<br>"; echo $observedWeatherResponse->detailedMessage . "</p>"; }
$newFieldResponse = makeAPICall('POST', 'https://api.awhere.com/v2/fields', $access_token, $newFieldStatusCode, $newFieldResponseHeaders, json_encode($newFieldBody), array("Content-Type: application/json")); } catch (Exception $e) { echo $e->getMessage(); exit; } if ($newFieldStatusCode == 201) { // Code 201 means the Create was successful echo '<p>A new field was created.</p>'; echo "<p>Request:</p><pre>POST https://api.awhere.com/v2/fields\n\n" . json_encode($newFieldBody, JSON_PRETTY_PRINT) . "</pre>"; echo '<p>Location Header (shows the URI of the new object):</p>'; // Anytime you create a new object, we return a Location header with the URL of where // you can get that object. You could save this value or use it in your next GET call // to retreive the thing you created. As a matter of convenience, though, our APIs // also return the object you created with this request. So it's a matter of preference // for your code/architecture. echo "<pre>" . parseHTTPHeaders($newFieldResponseHeaders, array('Location')) . "</pre>"; echo '<p>Response Body: (as a matter of convenience we send back the data that was created)</p>'; echo '<pre>'; echo stripslashes(json_encode($newFieldResponse, JSON_PRETTY_PRINT)); //Note: Stripslashes() is used just for prettier echo '</pre>'; //output in the browser. Not needed normally. //To show the newly created field we'll just repeate the Get Fields List call: try { $fieldsListResponse = makeAPICall('GET', 'https://api.awhere.com/v2/fields', $access_token, $fieldsListStatusCode, $fieldsListResponseHeaders); } catch (Exception $e) { echo $e->getMessage(); exit; } echo '<p>Get Fields List with Newly Created Field</p>'; if ($fieldsListStatusCode == 200) {