* * *

Author Topic: [SOLVED] how to "save as" result from PHP script ?  (Read 418 times)

mirce.vladimirov

  • Full Member
  • ***
  • Posts: 213
[SOLVED] how to "save as" result from PHP script ?
« on: June 20, 2017, 03:08:23 pm »
My application should open a HTTP link like "http://somepage.com/012301230123.php" , and this PHP script  prepares and returns a CSV file and I should retrieve the data stored within it, and finaly write the data to a database  table.
It's easey under Linux console, there is curl :
Code: [Select]
curl http://somepage.com/012301230123.php  > mycsvfile.csv

But my application should run under windows. Is there similar tool to be used under Lazarus ?
Note:I am already using Indy for ftp download/upload files, but I've never retrieved data like in this example.
« Last Edit: June 20, 2017, 03:36:32 pm by mirce.vladimirov »

turrican

  • New member
  • *
  • Posts: 46
  • Pascal is my life.
    • Homepage
Re: how to "save as" result from PHP script ?
« Reply #1 on: June 20, 2017, 03:29:03 pm »
You can implement Indy HTTPClient :

Code: Pascal  [Select]
  1. var
  2.   IdHTTP1: TIdHTTP;
  3.   CSV: TMemoryStream;
  4. begin
  5.   CSV := TMemoryStream.Create;
  6.   IdHTTP1 := TidHTTP.Create;
  7.   try
  8.     try
  9.       IdHTTP1.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1';
  10.       IdHTTP1.Get('http://http://somepage.com/012301230123.php', CSV);
  11.     except
  12.       on E: Exception do
  13.       begin
  14.         {$IFDEF DEBUG}ShowMessage('get error:'+E.Message){$ENDIF};
  15.       end;
  16.     end;
  17.     ShowMessage(IntToStr(CSV.Size));
  18.   finally
  19.     CSV.Free;
  20.     IdHTTP1.Free;
  21.   end;
  22. end;
  23.  
« Last Edit: June 20, 2017, 03:31:29 pm by turrican »

mirce.vladimirov

  • Full Member
  • ***
  • Posts: 213
Re: [SOLVED] how to "save as" result from PHP script ?
« Reply #2 on: June 20, 2017, 03:38:37 pm »
Thank you very much.

There are links that apeared AFTER I posted my question, sorry, I've searched previosly but none came up.
Here are the links :

http://wiki.freepascal.org/fphttpclient#Get_body_of_a_web_page_via_HTTP_protocol
http://forum.lazarus.freepascal.org/index.php/topic,29192.msg183851/topicseen.html

I will test Indy and this fphttpclient and post here what was best.
Thank you one more time.
« Last Edit: June 20, 2017, 03:40:41 pm by mirce.vladimirov »

turrican

  • New member
  • *
  • Posts: 46
  • Pascal is my life.
    • Homepage
Re: [SOLVED] how to "save as" result from PHP script ?
« Reply #3 on: June 21, 2017, 05:10:26 pm »
Hi!

No problem, you are welcome.

When client gets a response from the server take and eye to response's content type (application/json for example) for save as binary, text, etc...

mirce.vladimirov

  • Full Member
  • ***
  • Posts: 213
Re: [SOLVED] how to "save as" result from PHP script ?
« Reply #4 on: June 22, 2017, 12:57:49 pm »
Since I expect only *.csv data I decided to use fphttpclient. I believe that's the simplest solution, here's my code :
Code: Pascal  [Select]
  1. uses
  2.         ...., fphttpclient ;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.  myhttp : TFPCustomHTTPClient;
  7.  lista: TStringList;
  8.  
  9. begin
  10.  
  11.   myhttp := tfpcustomhttpclient.Create(Nil);
  12.   lista:=TStringList.Create;
  13.   lista.Clear;
  14.   lista.Add(myhttp.get('http://somewebsite.com/012301230123.php')) ;
  15.   lista.SaveToFile('c:/path_to_myfile/myfile.csv');
  16.   lista.Free;
  17.   myhttp.Free;
  18.   ShowMessage('Done !');
  19.  
  20. // now do everything else I need to do with the *.csv file
  21. end;
  22.  
  23.  

I will say one more time, I expect only text data in  *.csv files and  that I recieve that is the main reason why i decided for this. It's a different thing if response could contain binary data, no doubt that Indy is better for that.

rvk

  • Hero Member
  • *****
  • Posts: 2566
Re: [SOLVED] how to "save as" result from PHP script ?
« Reply #5 on: June 22, 2017, 01:12:40 pm »
I will say one more time, I expect only text data in  *.csv files and  that I recieve that is the main reason why i decided for this. It's a different thing if response could contain binary data, no doubt that Indy is better for that.
There are class functions in TFPHTTPClient which you can use without creating a instance yourself. Like TFPHTTPClient.SimpleGet(). You can call that directly

And if you use the version with TStream as result you can read binary files too.

So the simplest version would be:
Code: Pascal  [Select]
  1. uses fphttpclient;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   FileStream: TFileStream;
  6. begin
  7.   FileStream := TFileStream.Create('c:/path_to_myfile/myfile.csv', fmCreate);
  8.   try
  9.     TFPHTTPClient.SimpleGet('http://somewebsite.com/012301230123.php', FileStream);
  10.     ShowMessage('Done !');
  11.   finally
  12.     FileStream.Free;
  13.   end;
  14.  
  15.   // now do everything else I need to do with the *.csv file
  16.  
  17. end;

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus