B4J Tutorial [BANano] inline php -> server code

Discussion in 'B4J Tutorials' started by alwaysbusy, Jan 7, 2019.

Tags:
  1. alwaysbusy

    alwaysbusy Expert Licensed User

    Peter had a great idea for a new feature in BANano:
    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:
    Code:
    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.
    Code:
    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:
    Code:
    <?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: Jan 7, 2019
    Johan Hormaza, Don Oso, susu and 8 others like this.
  2. Kiffi

    Kiffi Well-Known Member Licensed User

    Wonderful! Thanks for the new version, Alain!

    I think Xojo and co have to prepare for hard times. :)
     
    ShaneG30 and alwaysbusy like this.
  3. ShaneG30

    ShaneG30 Well-Known Member Licensed User

    Two questions:
    1: Will this eventually lead to a B4PHP?
    2: Have you considered opening a Patreon account?
     
  4. alwaysbusy

    alwaysbusy Expert Licensed User

    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.

    I don't know that platform. I see it is for artists. Should it also apply to programmers?
     
  5. ShaneG30

    ShaneG30 Well-Known Member Licensed User

  6. OliverA

    OliverA Expert Licensed User

    ShaneG30, Kiffi and alwaysbusy like this.
  7. alwaysbusy

    alwaysbusy Expert Licensed User

    xulihang, ShaneG30 and OliverA like this.
  8. Cableguy

    Cableguy Expert Licensed 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....
     
    Harris, ShaneG30 and alwaysbusy like this.
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