Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

delphi

I have a similar question here, but it was oriented to PHP side. It looks like PHP is able to receive a package of data and immediately (in the same session) send and answer back. My question is can Delphi do that? From what I know the answer is a big NO. I need to do this in two steps (two procedures). The thing is that the session will be probably closed when the first procedure ends. Can I keep the session open between the two procedure calls.

like image 885
Server Overflow Avatar asked Dec 12 '22 15:12

Server Overflow


1 Answers

I'm going to give you sample code for both PHP and Delphi. We're going to use the GET request method because it's much easier and it's enough if you don't need to send too much data. I'm going to start with the PHP script, because it can be tested alone, without the Delphi app (we'll test it using an Browser).

Here's the PHP script:

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

This scripts expects two values sent encoded in the URL (a and b), multiplies the values and returns the answer. Assuming you're playing on the computer that's running LAMP, and you called this script script.php, if you navigate to this on your browser:

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

You'll see this in your browser:

6

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

Delphi code

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

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 proper working URL in there, so you can actually test against my known-to-work script.

Here's how you'd call this routine:

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

Summary

  • HTTP is stateless, people go to lots of trouble to maintain some kind of state. Your Delphi app makes two calls, first to SendID then a second to GetDataBack idea doesn't work, because the thing running on the server when you call (actually GET) GetDataBack has no idea someone sometime called (GET again) SendID.
  • Happily when GET-ing 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'd use the POST requests.
like image 121
Cosmin Prund Avatar answered Dec 15 '22 05:12

Cosmin Prund