Get Access Token"; 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 "
Access Token = $access_token
"; // MAKE API CALL // Now we'll make a simple API Call to get a list of fields in our account. // If this is the first time you've used the APIs this list will be empty. echo "There are '.count($fieldsListResponse->fields)." field locations in the current page of results.
"; echo 'Request:
GET https://api.awhere.com/v2/fields'; echo '
Content-Range Header (shows pagination and total results):
'; // 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: fields 0-5/5 echo "".parseHTTPHeaders($fieldsListResponseHeaders,array('Content-Range'))."";
echo 'Response Body:
'; echo ''; echo stripslashes(json_encode($fieldsListResponse,JSON_PRETTY_PRINT)); //Note: Stripslashes() is used just for prettier echo ''; //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 "
ERROR: ".$fieldsListStatusCode." - ".$fieldsListResponse->simpleMessage."
";
echo $fieldsListResponse->detailedMessage."