Frederick Posted March 3, 2020 Share Posted March 3, 2020 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? -- Frederick (UniGUI Complete - Professional Edition 1.90.0.1523) Quote Link to comment Share on other sites More sharing options...
Sherzod Posted March 4, 2020 Share Posted March 4, 2020 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? Quote Link to comment Share on other sites More sharing options...
Frederick Posted March 4, 2020 Author Share Posted March 4, 2020 This will be from the client side. Quote Link to comment Share on other sites More sharing options...
Frederick Posted March 6, 2020 Author Share Posted March 6, 2020 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. Quote Link to comment Share on other sites More sharing options...
erich.wanker Posted March 6, 2020 Share Posted March 6, 2020 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; 1 Quote Link to comment Share on other sites More sharing options...
Frederick Posted March 6, 2020 Author Share Posted March 6, 2020 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!". Quote Link to comment Share on other sites More sharing options...
Frederick Posted March 6, 2020 Author Share Posted March 6, 2020 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; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.