Check all API variables are included in a REST API for PHP.

I recently have been working on a weather recording project and as part of this endeavour wanted to check that all of the variables I have posted to my API were set, it can be rather a lot of work to have to specify each if (!isset($_GET[])) parameter so I decided to use a function that can take any number of arguments using a ‘variadic’ function in PHP.

// Returns true if all variables are set, else returns false.
function getVariablesSet(string ...$getlinks) {
      foreach ( $getlinks as $link ) {
        if (!isset($_GET[$link]))
        {
            return false;
        }
    }
    return true;
}

Now, when I want to use a new api ‘command’, I can simply do the following,

$browser_response = new stdClass();
$browser_response->message = "Command not specified.";

if ($_GET['command'] == "new-temperature")
    {
        if (getVariablesSet("datetime","temperature","humidity"))
        {
            $browser_response->message = "All GET variables set.";
        }
        else
        {
            $browser_response->message = "All GET variables not set.";
        }
    }

echo json_encode($browser_response);

This way, so long as we have specified the command and the variables required we can enter the scope, otherwise, we can kick them out until their query is formatted correctly. Having lots of variables may become problematic so you may want to use POST or even break them out into subsections to give users a better understanding of their error. I should add this only works for PHP 5.6 and above.

In any capacity, Good Luck. Aidan.

Woodmansy, Beverley UK Weather Current Temperature and Humidity

Hey Thanks for checking out my project, or discovering it organically!

Whats the weather?

This page shows the output of my outdoor box DHT22 Temperature sensor, powered by two 100w solar panels it can sometimes run out of power, especially late at night on miserable days. The data is updated in 15-minute intervals based on three samples. The data is monitored and transmitted from an esp8266 to my web-server, which is then logged in a google spreadsheet and then published here, only the last 500 entries are kept in the chart, which makes the whole chart 15 hours long.

The data on this chart can be better viewed here -> Link

The data on this chart can be better viewed here -> Link

If you would like historical data, please contact me.