Xojo talking to PHP little example request

Hi there. I need to read some php files within XoJo. Is it possible a colleague to give me a simple example eg entering myname in a textfield and then pass it to hello.php returning back “Hello, Mike” in a xojo textfield? I would appreciate this offer a lot! Thank you in advance.

Is Hello.php capable of exchanging data ?

You would use a http socket to trigger a php script by querying it’s url.
You can pass parameters and check results.

No Michael but maybe can create a file which then read by xojo or somehow, i m not experienced with web stuff.

Thanks Christian. Coukd you please give me a 2-3 lines exampe?

There is an example on the documentation site:

http://developer.xojo.com/xojo-net-httpsocket

But any example needs to be made specific to your php script.

Dim form As new Dictionary
Dim socket1 As New HTTPSocket

form.Value("Name") = FieldName.text
  socket1.SetFormData(form)
  Dim data As String = socket1.post("http://192.168.0.1/hello.php", 30)
  if instr(data, "null") = 0 then
    Dim apiResponse as new JSONItem(Data)
    dim results as JSONItem = apiResponse.child("data")
    dim n as JSONItem
    
     n = results.child(i)
     FieldName.text = n.value("Name")
  end if

Appreciate you all for the help, with special thanks to Tony for the time spend to write the code! I will try ti experiment later and sharing my thoughts about, here…

Tonny,I got an error when try to run. What is missing? Could you please help?

WebPage1.Open, line 12

This item does not exist
n = results.child(i)

Also with
http://192.168.0.1/hello.php
I get a null data object.
I changed to
"http://127.0.0.1:8080//hello.php
I got an error in: Dim apiResponse as new JSONItem(Data)
Parse Error: Expecting “{” or “[”

This is the code of hello.php. It is in root c:\

PHP Test <?php echo '

Hello,

'; ?>

Michael, that little Php is completely unable to get anything from anywhere. You need code to receive a post, or to get the URL parameters.

As for the path error, you need to do more to render a file accessible from the Internet with your app.
Read http://documentation.xojo.com/index.php/WebApplication.HandleURL

You need to get more familiar with Php, as well as Xojo Web…

  1. You don’t need all that HTML stuff. Just:
<?php echo 'hello';  ?>

will do.

  1. It looks like you are intending that the web server which will execute this PHP file is running on your own machine. In that case, you’ll need to be installing, running, and configuring a webserver. Apache 2.4 is an example. You’ll need a config file to tell apache a number of things when it starts up, such as which port to listen on, where to find any files such as hello.php, etc.

  2. If all that is set up properly, you’ll get the string ‘hello’ back from apache. For initial testing I wouldn’t bother with json, just put the returned string (data) into your textfield.

There are two big issues here.

  1. The small Hello program is completely unable to exchange any data, or get any data. Incidentally, that is the question I asked two days ago, as the very first reply to the original post. If you want hello to display your name, you need to make it able to do so.

This will do it :

<?php echo 'Hello '.$_GET['name'];  ?>

The program must be called with an URL parameter “name”, such as :

http://fontmenu.com/xojo/Hello.php/?name=Tim

And the result will be :
Hello Tim

  1. For a Php program to execute, it must be installed on a host that has Php enabled. Which is not the case of a standalone Xojo app. I am sorry, I wrote too fast, there is simply no way to execute a Php program through HandleURL which I suggested above. In order to execute a Php program, you need a web host with Php installed, such as what I did at fontmenu:

http://fontmenu.com/xojo/Hello.php/?name=Tim

You can put whatever name instead of Tim, and it will echo it.

So what you want to do is indeed possible, at the condition that you upload the php to the proper web space.

[quote=380251:@Michel Bujardet]There are two big issues here.

  1. The small Hello program is completely unable to exchange any data, or get any data. Incidentally, that is the question I asked two days ago, as the very first reply to the original post. If you want hello to display your name, you need to make it able to do so.

This will do it :

<?php echo 'Hello '.$_GET['name'];  ?>

The program must be called with an URL parameter “name”, such as :

http://fontmenu.com/xojo/Hello.php/?name=Tim

And the result will be :
Hello Tim
[/quote]

Well, the OP has to get the basic structure of his setup right, or he will get nothing. Making his PHP program do something more interesting can come later.

[quote]
2. For a Php program to execute, it must be installed on a host that has Php enabled. Which is not the case of a standalone Xojo app. I am sorry, I wrote too fast, there is simply no way to execute a Php program through HandleURL which I suggested above. In order to execute a Php program, you need a web host with Php installed, such as what I did at fontmenu:

http://fontmenu.com/xojo/Hello.php/?name=Tim

You can put whatever name instead of Tim, and it will echo it.

So what you want to do is indeed possible, at the condition that you upload the php to the proper web space.[/quote]

The OP says his test PHP file is on c:\. This means it’s on his own machine. If he wants to do that, which is perfectly feasible, that is why he has to obtain and run webserver software such as apache as I described. Once that is running, only then can his Xojo app run code as in Tony Lam’s example. That will send out an HTTP request, which is picked up by the apache server. If the server is configured right, it then finds and runs the PHP code, the output of which is returned to the Xojo app.

It was not clear to me whether his production setup will involve having the PHP program on his own machine, or a separate webserver as you describe.

I stuck to the OP description of what he wanted to do. Obviously, his PHP must be able to take the name in the TextField.

At any rate, I frankly don’t see the point in using Php to do what can be done very simply in Xojo Web.

If you just want to test a PHP script locally you can run it using PHP’s built in web server:

<?php
// router.php
if (preg_match('/\\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
    return false;    // serve the requested resource as-is.
} else { 
    echo "<p>Welcome to PHP</p>";
}
?>

$ php -S localhost:8000 router.php

Source

On Mac, Php is certainly available, if enabled, and can be used from the terminal, or the built in web server.

However, on PC under Windows 10, it is a tad more involved.

https://secure.php.net/manual/en/install.windows.php

Thank you all, a lot for your suggestions which are previous for people like me. I will install apache and php in my Windows for testing putposes but my intention is to run to Linux/Apache server. The point of using PHP is that i neef statistics and AI functions, something that really missed in Xojo framework. I could wtite the code for most of them but it will be tendious work and takes time fir testing extensively. If anybody has a solution to suggest apart of my idea gor using PHP i am glad to hear. I am not sure if one can wrap in xojo libraries the corresponding java statistical & ai libraries. This would be a big leap in developing Xojo web applications.

Ps i just yesterday bought my first Apple Macbook Air just for the sake of Xojo as i think that is optimisef for Macs.