Jump to content

How To Receive Values From A PHP Web Page?


Frederick

Recommended Posts

6 hours ago, Frederick said:

If I want to call a PHP web page that returns a string, which component can I use to call the page, wait for it to complete and then accept the string?

Do you want to make a request on the client or server side?

Link to comment
Share on other sites

On 3/4/2020 at 1:59 PM, Sherzod said:

Do you want to make a request on the client or server side?

Is it easier doing it from the server side? I don't care which method is used as long as I can get the return values from the PHP page and process it.

Link to comment
Share on other sites

in one of your uniForms (or Uniframes) create a function:

 

in uses add           IdHTTP

in private add       function talkwithphp(url_to_php:string):string;

 

function TUniform.talkwithphp(url_to_php:string):string;
const
  cUSER_AGENT = 'Mozilla/4.0 (MSIE 6.0; Windows NT 5.1)';
var
 httpResponse: string;
 Stream: TStringStream;
 URL, telParams:string;
 HTTP:TidHTTP;
begin
  URL:=url_to_php;
  Stream := TStringStream.Create;
  HTTP:=TIdHTTP.Create(nil);
  HTTP.ReadTimeout := 10000;{ IdTimeoutInfinite; }
  HTTP.ConnectTimeout := 10000;
  try
    HTTP.Request.UserAgent := cUSER_AGENT;
    try
      HTTP.Get(URL, Stream);
      result:= Stream.DataString;
    except
      result:= 'Communication Error!';
    end;
  finally
    Stream.Free;
    HTTP.Free;
  end;
end;

 

when make a onButtonClick or something ..

procedure TUniform.UniButton20Click(Sender: TObject);
begin
showmessage(talkwithphp('http://yourdomain.com/phps/myphp.php?value=123'));

end;

 

 

  • Like 1
Link to comment
Share on other sites

Hi Erich,

Thanks for the code. I tried it with a call to

showmessage(talkwithphp('https://www.ctsoftware.com.my/myprg/hello2.php');

and I received an error from Delphi as per the attached screenshot.

Calling the PHP file from the Opera web browser works fine.

I should see a message of "Hello world! What a nice day!".

readphp.png

Link to comment
Share on other sites

I solved the error by adding

idSSL, idSSLOpenSSL in the uses line and amending the function as follows:-

function TalkWithPhp(const url_to_php : String) : String;
const
  cUSER_AGENT = 'Mozilla/4.0 (MSIE 6.0; Windows NT 5.1)';
var
  Stream : TStringStream;
  URL : String;
  HTTP : TidHTTP;
  ioSSL : TidSSLIOHandlerSocketOpenSSL;   // Added this line
begin
  URL:=url_to_php;
  Stream:=TStringStream.Create;
  ioSSL:=TidSSLIOHandlerSocketOpenSSL.Create(NIL); // Added this line
  ioSSL.SSLOptions.SSLVersions:=[sslvTLSv1_2]; // Added this line
  HTTP:=TIdHTTP.Create(nil);
  HTTP.ReadTimeout:=10000{ IdTimeoutInfinite; }
  HTTP.ConnectTimeout:=10000;
  HTTP.IOHandler:=ioSSL;   // Added this line
  try
     HTTP.Request.UserAgent := cUSER_AGENT;
     try
       HTTP.Get(URL, Stream);
       result:= Stream.DataString;
     except
       result:= 'Communication Error!';
     end;
  finally
     Stream.Free;
     HTTP.Free;
     ioSSL.Free; // Added this line
  end;
end;

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...