0

I want to make a desktop application (Delphi) and an online PHP script. Each desktop application sends some unique identifiers to the PHP script and expects an answer (a record of data) from it. The record of data is generated instantly by the PHP script based on the unique identifier received.

I know enough to do the Delphi part, but I don't know much PHP. Can anybody give me some general lines about how I POST and retrieve data back from the PHP script?

Thanks


EDIT:

I am thinking at something like:

Desktop -> function SendID
PHP -> generate data (and make it persistent)
Desktop -> function GetDataBack

The only idea that pops in my mind is to make the PHP script create a file that has the same name as the ID received from the desktop application. Then the desktop app downloads this file and signal the server that the file can be deleted from the server.

Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • How are you expecting the return data? If you make your application handle the data as XML or JSON, PHP has native implementations that make it easy to return data in these formats... That way you would request infomormation from a url including your identifier (either by POST or GET) - example: http://www.yourthing.com/getdata.php?uid=112312, when you visit that page - it will return a string of XML or JSON that you can then process... – calumbrodie Jan 14 '11 at 10:20
  • Hi calumbrodie. I just want the data back. I don't know how. XML will do it. A simple TXT file where each piece of data is on a separate row will be even better. – Gabriel Jan 14 '11 at 10:32

3 Answers3

3

Your requirements seem pretty simple... a basic PHP script that solves your problem might look like this:

file: index.php


<?php

$posted_data = $_POST['your_variable_name'];

switch ($posted_data) {
    case "UID_APP1":
        echo "This is the data being returned for application 1."
        break;
    case "UID_APP2":
        echo "This is the data being returned for application 2."
        break;
    default:
        echo "This is the default data being returned.";
}

To run the PHP script on a server, you need a web server running. Probably, Apache2 will do this job for you since it is one of most commonly used webservers. Depending on your host operating system, there should be a package named apache2 and another one named libapache2-mod-php5. The basic configuration of apache to be able to run php scripts can be found on various websites. (just google for LAMP - "Linux Apache MySQL PHP") I hope this answers your quesion, if not, don't hesitate to ask.

Regards, Paul

Paul
  • 46
  • 1
  • great, could I answer your questions? – Paul Jan 14 '11 at 10:33
  • What I don't understand is how the script receives the ID from the desktop app and answers some data back at the same time? I think my desktop app needs to make two calls: function SendID and function GetDataBack. The moment I send the ID the PHP script outputs the data but until the second connection to the script is made to get the data back, the data is lost. This is why in my orig post I want to save the data to disk: so it is not lost until the second function (GetDataBack) is performed. Or PHP has the ability to receive data and send data back at the same time? – Gabriel Jan 14 '11 at 10:44
  • Of course, the example I posted before does receive AND send data. Receiving posted data is done via accessing the so called "Superglobal" variables $_POST, $_GET, ... (there are more which are not relevant right now). So for example if you call your.server.com/the.script.php?var=value (which is done by a HTTP GET request), you can access the string value by accessing the variable $_GET['var']. Some thing applies to POST. – Paul Jan 14 '11 at 10:59
1

In PHP

$delphiAnswer = $_POST['delphiAnswer'];
//DO SOME DATABASE STUFF OR HOWEVER YOU WANTED TO GET THE ANSWER
echo $newAnswerforDelphi;

Fairly simple really, however you need to be more specific about how you want to get answers

benhowdle89
  • 36,900
  • 69
  • 202
  • 331
1

Use $_REQUEST, $_POST or $_GET to get your parameters. See W3 Schools site for details. For output, simply echo what you want to return, in whatever format you want e.g.

<?php
$id = $_REQUEST['id'] ;

$data = /* get the data */

echo json_encode($data) ;

...for JSON.

Hogsmill
  • 1,574
  • 13
  • 21