Solved

Delphi -> POST -> PHP

Posted on 2001-09-05
8
1,715 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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