B4J Question B4J Web Server and C:\PHP\php-cgi.exe

Christos Dorotheou

Member
Licensed User
Longtime User
Hi everyone,


Is it possible to have a B4J Web Server configured, so it passes all *.php files from www to PHP (C:\PHP\php-cgi.exe) which is setup on the same box with the B4J Server ?

I know that in other web servers there are configuration settings to point where the PHP is and
settings to tell the server that all files with .php extension should be passed to the PHP.

Any help will be greatly appreciated.


Regards

Chris
 

Christos Dorotheou

Member
Licensed User
Longtime User
Start with a simpler task. Create a standard handler that receives a request and then uses jShell library to call the PHP program and return the result.

If you like I can post an updated version of jShell that supports synchronous execution which is simpler to use in a server app.

Thanks Erel,

I will wait for the updated version of jShell and I will try to follow your advise.

Regards

Chris
 
Upvote 0

giacomo-italy

Member
Licensed User
Longtime User
Hi Erel,
I tried to follow your advice and I was able to partially implement what you suggested. I installed php on my pc (windows 7 64 bit) (in C: \ PHP \ php-cgi.exe),
I implemented an handler with shell (i work on your exemples):

B4X:
Sub Process_Globals
    Private srvr As Server
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("srvr")
    srvr.Port = 8888
    srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
    srvr.LogsFileFolder = File.Combine(File.DirApp, "logs")
    srvr.AddHandler("/phppage", "phpPage", False)
   
    srvr.Start
    Log("Server started")
   
    StartMessageLoop
End Sub

and the class module 'phpPage':

B4X:
Sub Class_Globals
    Private mreq As ServletRequest 'ignore
    Private mresp As ServletResponse 'ignore
   
    Private StdOut1 As String

End Sub


Sub Handle(req As ServletRequest, resp As ServletResponse)
    mreq = req
    mresp = resp
   
   Dim shl As Shell
  
   shl.Initialize("shl", "php", Array As String("esempio2.php"))
  
   shl.WorkingDirectory = "C:\Users\utente\Desktop\web app\prova server gg1\Objects\www"
   shl.Run(10000) 'set a timeout of 10 seconds
   StartMessageLoop 'need to call this as this is a console app.

   resp.Write(StdOut1)
  
End Sub

Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success AND ExitCode = 0 Then
     Log("Success")
     Log(StdOut)
     StdOut1=StdOut
   Else
     Log("Error: " & StdErr)
   End If
   'ExitApplication
   StopMessageLoop
End Sub

(Note that I replaced 'ExitApplication' with 'StopMessageLoop')

then when i call in the browser: http://localhost:8888/phppage
the php code esempio2.php :

B4X:
<html>
<head>
  <title>Test PHP</title>
</head>
<body>
<?php echo "Hello World!<p>"; ?>
<?Php phpinfo();?>
</body>
</html>

runs successfully.

It was what you meant?
Because in this way i can execute a php file explicitly called by its name in the code and place in the www folder.
Can i execute a php file without indicating its name in the code?
I can not understand how to pass all *.php files from www to PHP, is it possible to do that?
It is possible execute in the srver php-code inserted in html file?
In other words it is possible for the server to work with php script as you would with an apache server (WAMP)?
Can you show some code to do these things?
Thank you very much for your answers.
 
Upvote 0

giacomo-italy

Member
Licensed User
Longtime User
Thank you for your answer Erel.
I know that my code is not very elegant, I used the expedient to define the string globals variable 'StdOut1' to pass the output of the Sub 'shl_ProcessCompleted' to the 'Sub Handle' for resp.Write(StdOut1)...

I tried to follow your advice:
I tried to replace shl.Run(10000) with shl.RunSynchronous(10000) so i have not the event 'ProcessCompleted' and it is not necessary to use Start/StopMessageLoop, but in this case how does the handler to show the result of the shell?

Can I fix my code or i am completely out of the way to solve the problem to run a php file? (even if it works).
Forgive my inexperience.
 
Upvote 0

giacomo-italy

Member
Licensed User
Longtime User
Thank you,
everything works fine.

To all those who have had the same need, here is the modified code
(class module 'phpPage'):

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)

   Dim shl As Shell
 
   shl.Initialize("shl", "php", Array As String("esempio2.php"))
 
   shl.WorkingDirectory = "C:\Users\utente\Desktop\web app\prova server gg1\Objects\www"
   
   Dim res As ShellSyncResult = shl.RunSynchronous(120000)
   'Log(res.StdOut)
   'Log(res.StdErr)
   resp.Write(res.StdOut)
 
End Sub

To all those who miss the php scripts (like me) i say that everything that can be done in php can be done in jquery, so let's go into the study of jquery...
This solution however allows us to run the *.php file in our server b4j, if necessary.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim res As ShellSyncResult = shl.RunSynchronous(120000)
Log(res.StdOut)
Log(res.StdErr)

that did not work for me...
shl.RunSynchronous is an unknown member of Shell

I´m using b4j 2.2 and Version
jshell001.png

1.10 of jshell


i cannot dim a ShellSyncResult too cause it is not selectable

jshell002.png


Do i miss a library? Or do i have an outdated version installed?
 
Upvote 0
Top