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.

Raspberry Pi – PHP permission File Problems Solved!

I recently installed php, but when I was unable to save files, I was very stuck, and that’s why I’m posting it here, I want to tell anyone who Googles this problem to find this solution.

 

The problem was when I used an fopen and fsave within php, the file would always ‘die’ as in the open or die within php. The solution is below

Firstly I looked for the Environment user in phpinfo() in my case it was
[box type=”shadow”]www-data[/box]
and then I simply typed this
[box type=”shadow”]chmod 777 -R www-data /var/www[/box]
that, in its simplicity is the solution, but I am thankful that I have found the solution!

To create a phpinfo you need to create a file in your root and insert the following code

and save it as phpinfo.php or whatever you choose, I saved it in my root but you can save it wherever you want, then I browsed to the environments section and that gave me the necessary information for me to create the solution.

Why this works

The problem was that php did not have enough permissions to write to the folder. by giving the user www-data full permission i.e. 777 it can now read write and execute without a problem!

here is what php looks like when you do phpinfo

phpinfo() user sample on Raspberry Pi

you can check a file exits and is editable with this code http://stackoverflow.com/a/13297218

Now you’ve finished you can celebrate! here is a song (this is mainly so i can test embedding :P)