-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path2-create-field.php
More file actions
142 lines (102 loc) · 5.5 KB
/
Copy path2-create-field.php
File metadata and controls
142 lines (102 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/* aWhere Code Samples
* Copyright (C) 2015 aWhere Inc.
* License: MIT
* Author: Jeof Oyster ([email protected])
*
* These code samples show a variety of different use cases and demonstrate how to
* make API calls in PHP. Each file shows a different use case. And each file
* is designed so that if you load the file to a browser and access it from a server,
* you will see prettified results in HTML.
*/
/* CODE SAMPLE: CREATE 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
// Next we'll create a field. Change the variables at the top of the
// script to customize the field (or create more than one).
echo "<hr><h1>Create a Field</h1>";
// For this example we're creating an associative array with the key-value pairs used in the payload.
// When we make the call we'll used json_encode() to turn it into a JSON-formatted string the API requires.
$newFieldBody = array( "id"=>$new_field_id,
"name"=>$new_field_name,
"farmId"=>$new_field_farm_id,
"centerPoint"=>array("latitude"=>$new_field_latitude,
"longitude"=>$new_field_longitude),
"acres"=>$new_field_acres);
try{
$newFieldResponse = makeAPICall('POST', //Use POST to create
'https://api.awhere.com/v2/fields', //URL
$access_token, //Access Token
$newFieldStatusCode, //Status Code (returned from function)
$newFieldResponseHeaders, //We want to capture the response HTTP headers
json_encode($newFieldBody), //Send the body as a json-formatted string
array("Content-Type: application/json") //The API requires an additional header to describe the payload.
);
} 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', //verb
'https://api.awhere.com/v2/fields', //URL
$access_token, //Access Token
$fieldsListStatusCode, //Status Code (returned from function)
$fieldsListResponseHeaders //We want to capture the response HTTP headers
);
} catch(Exception $e){
echo $e->getMessage();
exit();
}
echo '<p>Get Fields List with Newly Created Field</p>';
if($fieldsListStatusCode==200){
echo '<pre>';
echo stripslashes(json_encode($fieldsListResponse,JSON_PRETTY_PRINT));
echo '</pre>';
} else {
echo "<p>ERROR: ".$fieldsListStatusCode." - ".$fieldsListResponse->simpleMessage."<br>";
echo $fieldsListResponse->detailedMessage."</p>";
}
} else if($newFieldStatusCode==409){ // Code 409 means conflict - we use it when you try to create a duplicate object
// To run this code several times and create more than one field, you'll need to
// change the variables in header.php.
echo "<p>A field with ID $new_field_id already exists in your account, so it could not be created again.</p>";
} 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: ".$newFieldStatusCode." - ".$newFieldResponse->simpleMessage."<br>";
echo $newFieldResponse->detailedMessage."</p>";
}