1. 程式人生 > >Delphi通過Get獲取來自PHP的返回值

Delphi通過Get獲取來自PHP的返回值

Delphi程式碼

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,

  IdTCPClient, IdHTTP;

type

  TForm1 = class(TForm)

    Button1: TButton;

    IdHTTP1: TIdHTTP;

    Memo1: TMemo;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var

  idHttp1: TIdHTTP;

  stream: TStringStream;

begin

 stream := TStringStream.Create('');

 idHttp1 := TIdHTTP.Create(nil);

  try

  idHttp1.Get('http://localhost/DelphiRequest/index2.php', stream);

  Memo1.Lines.Add(UTF8Decode(stream.DataString));

  finally

  idHttp1.Free;

  stream.Free;

end;

end;

end.

//////////////////////////////////////////////////////////////////////////////////////PHP程式碼

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/16 0016
* Time: 下午 3:00 */ function test(){ $P="中文字元"; if(empty($_GET)){ return $P; }else{ return "cuowu"; } } echo test();