Android Example [B4X] PHP implementation of B4XSerializator and PhpCloudKVS Server example

Toky Olivier

Active Member
Licensed User
Hello,
First of all, sorry for my bad english... I'll try to do my best...

For my project, I ported the source of B4XSerializator of @Erel from here to php.
Even if php is considered as a weakly type language (before php 7), we can manage to work fully as expected with B4J and normally B4X (not tested yet with B4A and I don't have B4I).
It uses BinaryStream as found here: https://github.com/wapmorgan/BinaryStream

All types supported by B4XSerializator in B4X are supported but some workaround for some integer types, Array of Byte and Types.
Types that can be converted directly: integer, float, double, boolean, char, string, map (converted to associative array in php), list (converted to index based array in php), array (not byte array)

For byte array: As string in php is already a byte array, php can read directly byte arrays sent from B4X. But to send byte arrays from php to B4X, one class is available to hold the byte array variable that is read as normal Byte Array in B4X.
For example in the php side (the OutputBytes function echoes data from the server):
B4X:
$ser = new B4XSerializator();
$data = "this is a byte array data";
$ser->OutputBytes($ser->ConvertObjectToBytes(new B4XByteArray($data));
In the B4X side:
B4X:
Dim data() As Byte
Dim ser As B4XSerializator
data = ser.ConvertBytesToObject(stream_from_php)
For numeric types other than integer (byte, short, long), for reading in php, there is no problem but for writing (or sending to B4X), the B4XNumeric class should be used to force php to write at the corresponding bit size. Example in the Php Side:
B4X:
$ser = new B4XSerializator();
$age= 25; 'normally, age can be saved as Byte
$ser->OutputBytes($ser->ConvertObjectToBytes(new B4XNumeric($age, B4XSerializator::T_BYTE));
For custom B4XTypes, as implemented in the .Net version, there is also B4XType class that holds them with class name and an associative array with the fields names and values.
Example (In Php Side):
B4X:
$fields= array('Name'=>'Willy', 'Age'=>new B4XNumeric(67, B4XSerializator::T_SHORT), 'Married'=>true, 'Children' => 4);
$ser = new B4XSerializator();
$mytype = new B4XType('MyTypeFromPhp', $fields);
In B4X Side:
B4X:
Type MyTypeFromPhp(Name As String, Age As Short, Married As Boolean, Children As Int)
Known bug for B4XSerializator for php: I can't find what's wrong with floating numbers between -1 to 1 (example: 0.75). B4XSerializator for php cannot serialize them correctly. If someone can find how to solve it.

As full example to demonstrate the usage of B4XSerializator for php, see attached the PHP version of the CloudKVS:
phpcloudkvs.gif


Installations:
- Upload to your server or copy to the www directory of your LAMP with php installed the content after extraction of the file PhpCloudKVS_Server.zip
- With phpmyadmin, create one database: cloudkvs
- Import the content of the file data.sql (in the above zip file in the folder "sql")
- In the file CloudKVS.php, modify the configuration to connect to your database
- That is. Extract the attached PHPCloudKVS_Client example for B4J

Note: PHPCloudKVS uses MysqliDB wrapper for Mysqli found here to facilitate query building etc. but you can easely modify source to work without it.

What were changed from the original CloudKVS_Client from Erel?
- In the Main module (Int instead of Long): Type Item (UserField As String, KeyField As String, ValueField() As Byte, IdField As Int, TimeField As Int)
- In the Main module, for the initialization of ckvs: point to the file in your server: ckvs.Initialize(Me, "ckvs", "http://127.0.0.1/b4xserializator/CloudKVS.php")
- In the CreateItem function in the ClientKVS module (create a timestamp in Int format that can be used in php): i.TimeField = DateTime.Now/1000
- And that's all

The result in the data table inside the cloudkvs database:
data table.png


Enjoy!
 

Attachments

Last edited:

Mashiane

Expert
Licensed User
Hi Toky, thanks a lot for this. I need your help though with this to make it work for BANano.

1. I have created a universal class that enables one to use MSSQL, MySQL, SQLite so far. So I need the phpClouldKVS of a generic nature that one can easily port to use any database. This means if I want to use MSQL, It should be easy to plugin phpCloudKVS. Can you please advise how I can do this?
2. I'd like to explore the CRUD functionality of this using BANano, can you advise?

Thanks a lot

TheMash
 
Top