• Sample Apps

Create an API (PHP) Application Framework

This tutorial is for an App Inventor to PHP Application Framework.

It basically uses just the GetValue method of TinyWebDB using a valid JSON request built in a Procedure in App Inventor, and passing it to the PHP.

It will pass in something like, as an example:

["VALUE", "login",["user1","user1",""]]

This is then decoded and parsed:

// $tag contains the whole JSON string passed in
$messageType = json_decode($tag);
// get actual data passed in, as an Array due to the JSON format
$inboundData = $messageType[2];
// and the actual tag used in the message so it can be
// evaluated, and passed back
$realtag = $messageType[1];

$realtag is then assessed to see what it is the user is trying to do, and appropriate code run and resultant data returned. For example:

if ($realtag == "login" ) {
// Because this is a login, we know we passed
// the username and password in
// Obviously you would check against MySQL or
// whatever, but hard coded here for simplicity
if (($username=="user1") and ($password="user1")) {
$theData[0] = "123"; // Pass back the Record ID
} else {
$theData[0] = "0"; // Invalid Username or Password

In this example, it has a log-in page that if you try anything other than user1 as the username and password, it will tell you it’s an invalid log in, as seen in the code above.

If you log in successfully it will give you a menu of options, of which the first one will send some data to PHP for processing and returning, using an alternate code path to the code above as the $realtag will not be ‘login’, but something else instead. In this case ‘getSomeData’.

Regardless of the data to be returned based on the $realtag code path run, the data will be in the $theData Array which is simply JSON encoded along with the inbound tag so it can be appopriately handled when back in App Inventor.

$resultData = array("VALUE",$realtag,$theData);
$resultDataJSON = json_encode($resultData);
echo $resultDataJSON;

See the PHP download below for the full PHP source code.

All of the data returned, username/passords etc are hard coded in this example, for simplicity, but obviously you can use MySQL, or anything else.

The different screens used throughout the application are just vertical arrangements that are hidden and shown when appropriate.

Also in this tutorial I also use the notifier to show you what is being bundled and sent.

TRY IT. Scan the barcode to your Android phone:
CUSTOMIZE IT. Download the source code blocks.

  1. save the source file to your computer
  2. open the My Projects page in App Inventor
  3. select Upload Source
  4. choose this file
CUSTOMIZE THE API. Are you a programmer? If so, download the PHP source code for the API.

Have fun!!

Martin Keywood


31 Responses

  1. Can anyone give me some ideas on what this would be used for? For non programmers this is not obvious although I have closely read every tutorial on here.

    • Hi there,

      This evolved as I was working on a larger App that is an Android version of a website I set up for my son many years ago (swapmycards.co.uk).
      I wanted a way that I could call multiple php pages (or snippets of php code) depending on what I was doing.
      The ServiceURL part of TinyWebDB only points to a single PHP page and I although I guess you could programatically change that to a different page in App Inventor for every call out to PHP, I personally felt that was too much risk of failure and so wanted to have a single PHP page that I could call and have it react appropriately to what I wanted it to do by reacting to what was passed in, ie:
      if ($realtag == “login” ) {
      would do what was needed for login code, etc.
      This could be run it’s own code, or call external existing code, as I am doing in my larger demo.
      Then return data to App Inventor in a standard format.

      As for the App Inventor side, that was just a simple example of emulating multiple pages, with each one doing something different through the same PHP call due to the data passed in.

      You are right though, it is aimed more at programmers.

      Thanks a lot and hope this helps.


  2. Hello Mkeywood,

    I have tried everything, I cannot get this or php1test to work at all. If i point them to your default server it works. But soon as i change it to any of the 5 servers i have up and running, it returns false or no value. I have not changed the php scripts at all that you have listed on both projects. Do you have any suggestions?


    • Hi,

      I’m affraid I don’t know exactly what is causing that, but the problem will likely be that your PHP is failing, for one reason or another.
      It’s more than likely invalid PHP causing a Server Error (500), and nothing to do with App Inventor at all.

      In the replies to earlier messages on the first tutorial I have put a few hints and tips.
      I’ve collated them all into one list here.

      All of this is in a browser, and not in App Inventor:

      1. If you have cut/paste the PHP lines in, make sure the line wrapping (or apparent line wrapping) is valid, and that every line is valid PHP code
      2. Make sure your Server is using PHP5 if using json_encode, otherwise you will have to construct the equivalent string manually with something like: $resultDataJSON = ‘[“VALUE”,”$tag”,[“$theData[0]”,”$theData[1]”]]’;
      3. Try going to the page directly in the browser (the one you are pointing to in ServiceURL in TinyWebDB) and see what it returns. If you do my one there it will be something like [“VALUE”,””,[“bbbb”]].
      4. If it doesn’t return a JSON string like that (maybe even shows a 500 Server Error), then you need to try and identify what is going wrong
      5. I tend to comment out EVERYTHING and run it again in the browser to see if the 500 Server error goes away. It will, if it’s valid PHP
      6. Then I reintroduce a bit of code at a time and echo out variables etc and keep refreshing the browser page and keep repeating this until I find out what is breaking it, then fix it.
      7. Ultimately, you will have a PHP page that if you go to in the browser will return a JSON string like that above. When it’s returning that, your App Inventor error will go away and you just need to make sure you’re handling the data correct in App Inventor, as per the tutorial.
      8. Also, on a side note, I have had times where the emulator does not work, but if I use the phone it will work fine. It seems random and unpredictable, so I use the phone every time now.

      In essence, the PHP is really minor and if it runs in a browser and returns a valid JSON string, then all is good. If not, I’m afraid it’s a PHP bug that needs to be tracked down in the usual time consuming way as any PHP I’m afraid.

      Good luck


  3. Mkeywood,

    I am showing this in my httpd logs
    test2.php/getvalue – 80 – IP – 200 0 0 1317

    Shouldn’t it be requesting test2.php?getvalue instead of whats showing above?

    • Hi Aaron,

      That is what you’re getting from a Post via App Inventor, right?

      That is right, though.

      If I modify that PHP page to capture the POST string to a file with the following code:

      $myFile = “testFile.txt”;
      $fh = fopen($myFile, ‘w’) or die(“can’t open file”);
      fwrite($fh, str_replace(‘”‘, ”, $postUrl));

      and do either a get or store I get /appinventor/test1.php/getvalue or /appinventor/test1.php/storeavalue respectively output to that file.

      Is it that $tag and $value are coming through as null?
      Have you tried getting them directly from the POST array instead:
      $tag =trim($_POST[“tag”]);
      $value =trim($_POST[“value”]);

      Someone recently had a similar problem where a new PHP file created in Notepad appeared to run properly but was not allowing access to the POST variables (all just null), but using an alternate editor to create the file worked fine. It was very strange and took a while to find, but completely true, so I thought I’d mention it in case it was something equally as bizarre here.

      Also, I have had wierd problems using the emulator too, so might be worth using an actual phone if you’re not already.

      Other than that, I’m out of suggestions I’m affraid 😦

      Hope this works for you.

      All the best


  4. Some hosters do seem to pass those ($tag, $value) global variables others don’t..

    It works with me with one hoster, while it does not with 110mb.

    Maybe a security setting for externally set globals.

  5. Is it possible to do this with ASP? I don’t know much PHP, but would like to connect this to my site.

    • Hi. I’ve never tried it with ASP but in theory it should be the same. Ie pick up the post variables ‘tag’ and ‘value’ and return just a JSON string.
      Best of luck

  6. Anyone know if there is a specific server setting that needs to be enabled to get this working?

    For some reason, my hostmonster.com server isnt grabbing the tag using

    $messageType = json_decode($tag);

    I tried the few suggestions above and still not working.


    • I’m afraid not. I use freehostia.com. That works ok but I don’t know about any low level server settings.
      One thing that springs to mind is to check your server is using PHP5 and json_decode works. Ie can you just get to $tag or $_POST(‘tag’) ?
      Does anyone else know of any server settings?
      All the best

      • I tried $_POST(‘tag’) which generally doesnt work on my server. $_REQUEST(‘tag’) normally works, but not here either.

    • I figured out my issue.

      First I sent the following text in as this is the format needed

      My output was

      With the following code my output is proper

      $messageType = json_decode(stripslashes($_REQUEST[tag]));


      I can now log in.

      • Thank you so much David for coming back and posting this… I would have never found this and after replacing the $messageType with your mine is working on my GoDaddy server.

        And of course thank you Martin for this great guide since using your project to reverse engineer what I makes things exponentially easier than reading all the very vague guides and patchwork information that is out there.

      • thank you this resolved my problem too.
        And huge thanx to original poster. Very cool stuff
        great that all the source was included

  7. mkeywood,

    I have been fooling around with the App InVentor and have been trying to devise a way to use php to talk to a MYSQL database using the tinywebdb feature in the App Inventor.

    First off, is this possible, secondly, if so, any suggestions?

    • Hi,
      The examples on this site allow passing of information to and from PHP. That can be data pulled via MySQL you call in the PHP.
      Alternately there are lots of examples on the official Google Appinventor group.
      All the best.

  8. You should use ($password==”user1″) instead of ($password=”user1″). Otherwise the password will always return true.

  9. I’m using an emulator, w/o a device. how can I get the source code?

    • Hi
      The downloads of the source can be imported into app inventor and used with any device that is connected through the blocks editor.
      One note though is that I personally found the php connectivity a bit flaky when using the emulator.
      Hope this helps

  10. Hi Martin
    I am doing a school project to make an app via eclipse to store and retrieve info. As you have helped to create the server end, I am coding on the client side on Android. I can do it in app inventor but i need to have the java codes for submitting. How can I call this method to enter into the website to get values or store values in java? Please advice as I am not a programmer and have difficulties coding in java.

    • Hi. To my knowledge there is no way of getting java source code for the app inventor apps, and I’m afraid I don’t have any java examples that do the same thing. Sorry about that.
      Hopefully someone else reading this will be able to help.
      All the best

      • hi martin
        thank you for ur response. im looking at Httpclient to enter the website to store and retrieve the values. But i cannot seem to target the correct tabs. can i ask how u name your editbox? As i saw that the name for the retrieve and store tabs are both named “tag”.

      • Hi,
        I’m sorry I don’t fully understand your question as I’m not too up on the java sdk side. I don’t think I can help 😦
        The php, as you see, is pretty basic but I’m not sure how it would need to change to accommodate a direct sdk call.
        I hope you get it sorted.
        All the best

  11. I just wanted to say that this is EXACTLY what I was looking for! Without much trouble I was able to have it connect to a database a verify login against that. And I don’t know much PHP at all but changing your script was fairly straight forward after a few Google searches on “PHP MYSQL queries”.

    Plus I can customize this. Wow. Now the possiblities and wide open!

  12. I can’t get language specific characters to work, it returns “null”, for the values of æ, ø and å (which are 3 danish characters).

    Any thoughts?

    Otherwise – really cool tutorial, awsome cool! 🙂

    • Hi,
      Thanks 🙂
      I’m sorry but I’m not sure what’s happening there.
      Are you storing the data in a database or just the text file?
      Is it OK if you look at it in the database or textfile? Or wrong there too?
      Just wondering if it’s wrong inbound to php and / or outbound from php?
      I’m not sure what App Inventors multi language support is like right now.
      Hope you get it sorted.
      All the best

  13. Hi

    I solved it – it was in the returning of variables, from php to app inventor.

    It only took the utf8 encode function, to make it right:
    $theData[0] = utf8_encode(“Pære”);

  14. Hi. I’ve spent a lot of time trying to get my app to write to a MS SQL DB. I’ve finally have my app calling my php page and it’s writing to the database, but the “tag” and “value” variables are just blank. I’m not sure what i’m doing wrong but I’ve tried so many different things. The value is not null, but is just blank. I have an Identity column and I can see that it is writing the record to the database correctly.

    Any thoughts? I’d be happy to post my PHP if you or someone would not mind taking a look. I’m about out of options.. 😦


    • Hi,
      Earlier in the discussion thread on both php examples are some hints and tips about debugging, and also some great feedback from other readers about what they have had to change to get tag and value to pass through to their php.
      Please feel free to paste your code here though, and we can take a look and see if anyone can help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: