B4J Tutorial [BANano] inline php -> server code

Peter had a great idea for a new feature in BANano:
Kiffi said:
Hello Alain,

So you don't get bored ;), I have an idea for another BANano killer feature: ServerCode :)

Imagine, it would be possible to write BANano code that could be used to program on both the client and server sides. Code blocks to be executed on the server side are bracketed with #If #End If (like JavaScript blocks).
...
I liked it! He had already written out the ground rules so we only needed a couple of mails to get on the same page. As a result BANano 1.27 has a PHP connection (we plan support for .asp(x), CGI, python, ... later).

A note beforehand: It is always an Ajax POST command, using JSON to do the communication. This may be extended in the future.

Some new settings one can set in AppStart:
B4X:
BANano.PHP_NAME = "myapi.php"
BANano.PHPHost = "http://localhost"
BANano.PHPAddHeader("Access-Control-Allow-Origin: *")

A very simple example using the new CallInlinePHPWait() method. All we have to pass is the php method name, and a Map with key pairs where: the key is the parameter name and the value the parameter value.
B4X:
public Sub ButtonClicked(event As BANanoEvent)
   ' function name and params are Case Sensitive!
   Dim res As String = BANano.CallInlinePHPWait("SayHello", CreateMap("Name": "BANano"))
   log(res)
End Sub

' a simple php method which says hello!
#if PHP
function SayHello($Name) {
   $ret = Array("answer" => "Hello " .$Name. "!");
   echo json_encode($ret);  
}
#End If

When Building the BANano app, an extra php file will be generated. All one has to do is copy this php file to e.g. a WAMPs /www/ folder et voila: both are talking with each other :).

An example of a generated php file:
B4X:
<?php
  header("Access-Control-Allow-Origin: *");
 
  $rest_json = file_get_contents("php://input");
  $_POST = json_decode($rest_json, true);
 
  $request='';
  if(isset($_POST['request'])){
      $request = $_POST['request'];
      $params = $_POST['params'];
  }

  if (!function_exists($request)) die("invalid request: '" . $request . "'");

  function SayHello($Name) {
     $ret = Array("answer" => "Hello " .$Name. "!");
     echo json_encode($ret);  
  }
 
  $values = array_values($params);
  call_user_func_array($request, $values);
?>

I personally still favour using B4J's jServer to handle all my server related things (it is really powerful and has proven to be quite a bit faster than e.g. php), but if someone needs a BANano solution that just can't affort to use a VPS, there is now an alternative.

BANano 1.27 can be downloaded here: https://www.b4x.com/android/forum/threads/banano-progressive-web-app-library.99740

Alain
 
Last edited:

LWGShane

Well-Known Member
Licensed User
Longtime User
Two questions:
1: Will this eventually lead to a B4PHP?
2: Have you considered opening a Patreon account?
 

alwaysbusy

Expert
Licensed User
Longtime User
Will this eventually lead to a B4PHP
Definitely something I have been thinking about last week. I don't think the transpiling would be a problem, just how to distinguish which B4J code needs to be transpiled to Javascript, and which to PHP.

Have you considered opening a Patreon account?
I don't know that platform. I see it is for artists. Should it also apply to programmers?
 

LWGShane

Well-Known Member
Licensed User
Longtime User

OliverA

Expert
Licensed User
Longtime User

Cableguy

Expert
Licensed User
Longtime User
Patreon is way beyhond being "just for artist"! Almost every youtuber, worthy of such a name, has a patreon account, as main "please keep on doing what you are doing cause you do it good!"...

SO… KEEP ON DOING....
 

giannimaione

Well-Known Member
Licensed User
Longtime User
sorry for post dated 2019;

i test example with a lib fpdf.php here
B4X:
banano.CallInlinePHP("TestPDF", CreateMap("Name": "test"), "ID1")
'
Sub BANano_CallInlinePHPResult(Success As Boolean, UniqueID As String, Result As String)
    Log(Success)
    Log(UniqueID)
    Log(Result)
'maybe I have to manipulate the result ?
End Sub
'
#if PHP
function TestPDF($Name) {
    require('fpdf.php');
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Prova pdf!');
    $pdf->Close();
    $pdf->Output('example_006.pdf', 'D');
}
#End If

log Succes is True
log Result is:
B4X:
%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Filter /FlateDecode /Length 70>>
stream
......
......
endstream
endobj
6 0 obj
<</Type /Font
/BaseFont /Helvetica-Bold
/Subtype /Type1
/Encoding /WinAnsiEncoding
/ToUnicode 5 0 R
>>
endobj
2 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 6 0 R
>>
/XObject <<
>>
>>
endobj
7 0 obj
<<
/Producer (FPDF 1.86)
/CreationDate (D:20240627061938+00'00')
>>
endobj
8 0 obj
<<
/Type /Catalog
/Pages 1 0 R
>>
endobj
xref
0 9
0000000000 65535 f
0000000226 00000 n
0000000865 00000 n
0000000009 00000 n
0000000087 00000 n
0000000313 00000 n
0000000747 00000 n
0000000969 00000 n
0000001052 00000 n
trailer
<<
/Size 9
/Root 8 0 R
/Info 7 0 R
>>
startxref
1101
%EOF
but the pdf file is not generated;

testing ONLY php script (test.php), works well
localhost/test.php

maybe I have to manipulate the result ?
 
Top