How to send data to a PHP script and imediatelly get data back from the same script

I have a similar question here , but it was oriented towards the PHP side. It looks like PHP can receive a data packet and immediately (in the same session) send and respond. My question is: can Delphi do this? From what I know, the answer is a big NO. I need to do this in two stages (two procedures). The fact is that the session is likely to be closed when the first procedure ends. Can I keep a session open between two procedure calls.

+4
source share
1 answer

I will give you sample code for PHP and Delphi . We will use the GET request method because it is much simpler and sufficient if you do not need to send too much data. I will start with a PHP script because it can be tested alone, without the Delphi application (we will test it using a browser).

Here's the PHP script:

 <?php $a=$_GET['a']; $b=$_GET['b']; echo intval($a)*intval($b); ?> 

These scripts expect the two values ​​to be sent to the URL (a and b), multiplied by the values ​​and return a response. Assuming you are playing on a computer running LAMP, and you called this script script.php if you go to this in your browser:

 http://localhost/script.php?a=2&b=3 

You will see this in your browser:

 6 

To break down the URL that we write in the browser: we request the /script.php page from the localhost server and we pass this request string to the script: a=2&b=3 . The material that comes after ? in the URL, is a query string; The & symbol separates two separate pairs of parameters = value: a=2 and b=3 . On the PHP side, you use $a=$_GET['a'] to get the value of parameter a . It's simple!

Delphi Code

Now that we know that the PHP script is up and running, we can move on to the Delphi part. Here's a procedure that multiplies two values ​​using the most inefficient method you can imagine:

 function MultiplyTwoNumbers(a,b:Integer):Integer; var url: string; H: TIdHttp; SS: TStringStream; begin // Prepare the URL url := 'http://fisiere.sediu.ro/script.php?a=' + IntToStr(a) + '&b=' + IntToStr(b); H := TIdHttp.Create(nil); try SS := TStringStream.Create; try H.Get(url, SS); Result := StrToInt(SS.DataString); finally SS.Free; end; finally H.Free; end; end; 

I intentionally left my correct working URL there, so you can actually test my famous script work.

Here you can name this procedure:

 procedure TForm8.Button1Click(Sender: TObject); begin ShowMessage(IntToStr(MultiplyTwoNumbers(3,4))); end; 

Summary

  • HTTP has no citizenship, people go to many problems in order to maintain some kind of state. Your idea is a Delphi app makes two calls, first to SendID then a second to GetDataBack does not work, because the thing running on the server when you call (actually GET) GetDataBack has no idea that someone ever called ( GET again) SendID .
  • Fortunately, when GET deletes data from an HTTP server, we can use the query string to easily send some information. If we need to send a lot of information, we will use POST requests.
+11
source

Source: https://habr.com/ru/post/1340433/


All Articles