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

Discussion in 'Tutorials & Examples' started by Toky Olivier, Aug 20, 2019.

  1. Toky Olivier

    Toky Olivier 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):
    Code:
    $ser = new B4XSerializator();
    $data = 
    "this is a byte array data";
    $ser->OutputBytes($ser->ConvertObjectToBytes(new B4XByteArray($data));
    In the B4X side:
    Code:
    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:
    Code:
    $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):
    Code:
    $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:
    Code:
    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!
     

    Attached Files:

    Last edited: Aug 21, 2019
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice