Solved

Delphi -> POST -> PHP

Posted on 2001-09-05
8
1,718 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Accepted Solution

by:
EddieShipman earned 50 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

679 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