A lot of developers like me use php as the backend component on a server to communicate with B4x apps. My intention is to create a thread with some "How to's" and best practices. Please feel free to expand it. It's not the place to post questions (please open a new thread then). The examples posted here can be used in all B4x products (maybe with a slight change).
All examples are simplified! Of course I use secured scripts (password hashing, encryption and some other methods)
If you are new to PHP, see this nice website. It has thousands of PHP examples with and without MySQL: https://www.w3schools.com/php/default.asp
1. Test environment
I use XAMPP https://www.apachefriends.org/de/index.html on my laptop I use developing my apps. Just install the version you need. It comes with a full package (Apache Server and MySQL). Just set the root password and start Apache and MySQL from the console or chose to run it as a service (red crosses on the left side). Nothing else to do here. Just run it. It listens to the standard ports. If it does not start, maybe another program is using port 80.
I have installed it a lot of times without any problems. It comes as a "ready to run" package. Please start it as an ADMIN and check your firewall settings to accept incoming connections!
Xampp installs directly on the boot drive (usually C:/). Put the PHP scripts in the htdocs folder and you are ready to go
2. Prod environment
After testing you can copy the php scripts to your prod server (mostly via FTP). Please ensure you change e.g. Database names, etc. (if you use MySQL or similar). Very easy. See your hoster's documentation how to access (paths) the scripts
3. B4x: Send data to a php script very easy
I use OKHttpUtils to call the scripts. Just use a variable to set the server's ip/address like
After some years of experience I use the POST-method to send data to my php scripts. All the data is put in a list containing maps which I convert into a nice JSON string.
- It's easy to handle ("just a string")
. It's very dynamic and can be expanded by just adding more maps
- One can encrypt/decrypt it in one step
- Usable for all data (even files/images, etc.)
So right here we are sending just a string ("JSONstring)
On the PHP side a list with maps is just an array containing arrays:
Steps here:
- get the posted data (get_contents...)
- decode it and put it into an array ($jsall) = list
- get the first array ($jsone) = out map
You could send more maps just by adding the to the list and use a loop in php to process all the maps. In my example I only send ONE map and I get it by the index 0.
To access the data from the map (array) just use the map's key (like we know it from B4x):
4. Limitations
On the serverside there are size limitations (set by the admin). 1-5 MB shouldn't be a problem. Just test it
5. No barriers
You can send even binary data like files, pics, etc. (Encode it to Base64 before you send it and decode it in the php script).
6. Important: Add security!
Trust no data! Check it all! Encrypt everything!
All examples are simplified! Of course I use secured scripts (password hashing, encryption and some other methods)
If you are new to PHP, see this nice website. It has thousands of PHP examples with and without MySQL: https://www.w3schools.com/php/default.asp
1. Test environment
I use XAMPP https://www.apachefriends.org/de/index.html on my laptop I use developing my apps. Just install the version you need. It comes with a full package (Apache Server and MySQL). Just set the root password and start Apache and MySQL from the console or chose to run it as a service (red crosses on the left side). Nothing else to do here. Just run it. It listens to the standard ports. If it does not start, maybe another program is using port 80.
I have installed it a lot of times without any problems. It comes as a "ready to run" package. Please start it as an ADMIN and check your firewall settings to accept incoming connections!
Xampp installs directly on the boot drive (usually C:/). Put the PHP scripts in the htdocs folder and you are ready to go
2. Prod environment
After testing you can copy the php scripts to your prod server (mostly via FTP). Please ensure you change e.g. Database names, etc. (if you use MySQL or similar). Very easy. See your hoster's documentation how to access (paths) the scripts
3. B4x: Send data to a php script very easy
I use OKHttpUtils to call the scripts. Just use a variable to set the server's ip/address like
B4X:
'servername="https://example.com" 'prod
'Servername="http://192.168.178.23" 'test -> where Xampp runs
After some years of experience I use the POST-method to send data to my php scripts. All the data is put in a list containing maps which I convert into a nice JSON string.
- It's easy to handle ("just a string")
. It's very dynamic and can be expanded by just adding more maps
- One can encrypt/decrypt it in one step
- Usable for all data (even files/images, etc.)
B4X:
JsonList.Initialize
JsonMap.Initialize
JsonMap.put("uname", "Klaus")
JsonMap.put("upw", "test")
JsonMap.put("umail", "kk@kk.de")
JsonList.add(JsonZeileMap)
Dim JSONGenerator As JSONGenerator
JSONGenerator.Initialize2(JsonList)
Dim JSONstring As String
JSONstring = JSONGenerator.ToString
Log(JSONstring)
Dim LoginJob As HttpJob
LoginJob.Initialize("LoginJob", Me)
LoginJob.PostString(Servername & "/login/login.php", JSONstring)
So right here we are sending just a string ("JSONstring)
On the PHP side a list with maps is just an array containing arrays:
B4X:
$json = file_get_contents("php://input"); 'get all the data sent by the app and put it in a string
$jsall = array(); 'the list we send
$jsone = array(); 'one map inside the list (we could send a list containing n maps)
$jsall=json_decode($json, true); 'decode the json string we have sent.
$jsone=$jsall[0]; 'get the first map (index=0)
Steps here:
- get the posted data (get_contents...)
- decode it and put it into an array ($jsall) = list
- get the first array ($jsone) = out map
You could send more maps just by adding the to the list and use a loop in php to process all the maps. In my example I only send ONE map and I get it by the index 0.
To access the data from the map (array) just use the map's key (like we know it from B4x):
B4X:
$umail=$jsone["umail"];
$upw=$jsone["upw"];
$uname=$jsone["uname"];
4. Limitations
On the serverside there are size limitations (set by the admin). 1-5 MB shouldn't be a problem. Just test it
5. No barriers
You can send even binary data like files, pics, etc. (Encode it to Base64 before you send it and decode it in the php script).
6. Important: Add security!
Trust no data! Check it all! Encrypt everything!