Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi -> POST -> PHP

Posted on 2001-09-05
8
Medium Priority
?
1,736 Views
Last Modified: 2012-05-05
Hello,

I wrote an delphi application, that send via NMHTTP
data to the php-script (internet).

The PHP-Script should use this variables, for example
i send an string "test=abc"  the script should output
abc  ( echo $test)

But the variable is empty. When I send it via ?test=abc  the output is right. The POST via Delphi's NMHTTP don't work as I want ...

can anyone help me?

Thanx
Andy
0
Comment
Question by:andys79
  • 4
  • 2
  • 2
8 Comments
 
LVL 8

Expert Comment

by:us111
ID: 6459809
first, in a simple HTML form, does the PHP post method work ??
I don't know NMHTTP, but I think it should work if NMHTTP support the post method
0
 

Author Comment

by:andys79
ID: 6460034
Hi,

for example to show my problem:

test1.html
-----------
<form name="test" method="POST" action="test2.php">
<input type="hidden" name="helloworld" value="yes">
<input type="submit">
</form>

test2.php
---------
<?php
echo "Skript running ...";
echo $helloworld;
// or: echo $HTTP_POST_VARS["helloworld"];
?>


=> OUTPUT: Skript running ...yes
-------------

the output is correct.
But when I use this script (test2.php) in Delphi via NMHTTP

     NMHTTP1.Post(Edit1.Text, 'helloword=yes');

then I get this:

=> OUTPUT: Skript running ...

(Edit1.Text is the URL of the php-internet-script)

I cannot access to the POST-Variable helloworld, or should I define the POST-Variables in another way in delphi?

Thanx

Andy

0
 
LVL 8

Expert Comment

by:us111
ID: 6460156
I believe that there's a problem with
NMHTTP1.Post(Edit1.Text, 'helloword=yes');

try
NMHTTP1.Get(Edit1.Text, 'helloword=yes');
if it exists :)

I don't know delphi
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:andys79
ID: 6463637
It doesn't work ...
(Too much parameter) ;-((
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 200 total points
ID: 6610916
Andy,
  I believe I saw your post on DelphiPages.com. I have been
taking a look at this and here's what I came up with.
Either you have to use GET with NMHTTP or Download Indy
and use Post... Here is an example program with NMHTTP
and Indy components showing how to do what you want...


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Psock, NMHttp, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, idGlobal;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    NMHTTP1: TNMHTTP;
    Label1: TLabel;
    Memo1: TMemo;
    IdHTTP1: TIdHTTP;
    Button2: TButton;
    Button3: TButton;
    Label2: TLabel;
    Memo2: TMemo;
    Memo3: TMemo;
    Button4: TButton;
    Memo4: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure NMHTTP1Success(Cmd: CmdType);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure NMHTTP1AboutToSend(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  NMHTTP1.OutputFileMode := False;
  NMHTTP1.Get(Edit1.Text + '?helloworld=yes');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  NMHTTP1.OutputFileMode := False;

  NMHTTP1.Post(Edit1.Text, '?helloworld=yes');
end;

procedure TForm1.NMHTTP1Success(Cmd: CmdType);
begin
  case Cmd of
  CmdPOST:
  begin
    Memo2.Text := NMHTTP1.Header;
    Label2.Caption := NMHTTP1.Body;
  end;
  CmdGET:
  begin
    Memo1.Text := NMHTTP1.Header;
    Label1.Caption := NMHTTP1.Body;
  end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  mStream: TMemoryStream;
begin
  mStream := TMemoryStream.Create;
  IdHTTP1.Get(Edit1.Text + '?helloworld=yes', mStream);
  mStream.Position := 0;
  Memo3.Lines.LoadFromStream(mStream);
  mStream.Free;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  aStream: TMemoryStream;
  Params: TStringStream;
begin
  aStream := TMemoryStream.create;
  Params := TStringStream.create('');
  idHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
  try
    Params.WriteString(URLEncode('helloworld=' + 'yes'));
    with idHTTP1 do begin
      try
        Post(Edit1.Text, Params, aStream);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message);
      end;
    end;
    aStream.Position := 0;
   Memo4.Lines.LoadFromStream(aStream);
  except
  end;
end;

procedure TForm1.NMHTTP1AboutToSend(Sender: TObject);
begin
  // I tried changing the content type here like in the Indy
  // component but still no go.
  NMHTTP1.SendHeader.Values['Content-Type'] := 'application/x-www-form-urlencoded';
end;

end.
0
 

Author Comment

by:andys79
ID: 6618935
Can a webserver accept posts from another application or is there a security mechanism?
0
 

Author Comment

by:andys79
ID: 6618936
Can a webserver accept posts from another application or is there a security mechanism?
Sorry, I was in holiday ..
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 6619014
Yes, as far as it's concerned, it is just another browser.

The example I show above works. I wrote a PHP program
just as you did and used the Delphi source above to
retrieve the value from the server.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question