Delphi -> POST -> PHP

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
andys79Asked:
Who is Participating?
 
Eddie ShipmanConnect With a Mentor All-around developerCommented:
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
 
us111Commented:
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
 
andys79Author Commented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
us111Commented:
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
 
andys79Author Commented:
It doesn't work ...
(Too much parameter) ;-((
0
 
andys79Author Commented:
Can a webserver accept posts from another application or is there a security mechanism?
0
 
andys79Author Commented:
Can a webserver accept posts from another application or is there a security mechanism?
Sorry, I was in holiday ..
0
 
Eddie ShipmanAll-around developerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.