delphi通过Idhttp和php交互


本文整理自网络,侵删。

 最近需要做delphi和php交互的方法;

就把这2个方法写了下

一,Get方法

const Url = 'http://www.delphitop.com';
procedure TForm1.Button1Click(Sender: TObject);
var
  stream: TStringStream;
  idHttpObj: TIdHTTP;
begin
try
  stream := TStringStream.Create('', TEncoding.UTF8); //中文就不会乱码了
  idHttpObj := TIdHTTP.Create(nil);
  idHttpObj.Get(Url, stream);
  Memo1.Text := stream.DataString;
finally
  idHttpObj.Free;
  stream.Free;
end;
二,POST方法,优点是比Get安全,我是通过json格式传送的;

(*********************************************************
    获取web返回的信息
    paramJson 参数,格式如下
    '{"name":"myname","ID":"12356"}'
*********************************************************)
function GetWebData(URL:string;paramJson:string):string;stdcall;
var
  slist : TStringList;
  http : TIdHttp;
begin
  try
    http := TIdHttp.Create;
    slist := TStringList.Create;
    slist.Text := 'json='+paramJson;//加入json,方便php调用
    result := http.Post(URL,slist);
  finally
    http.Free;
    slist.Free;
  end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
  memo1.lines.Add( GetWebData(URL,'{"key":"DFDGBFSE$#$$R","name":"中文","id":123456}') );
end;
  

通过PHP获取delphi发生的数据

<?php
    $value = json_decode( $_POST['json'] );
    var_dump($value);
?>

相关阅读 >>

Delphi tfdmemtable 更新到数据库

Delphi 从 twebbrowser中获得当前输入处的链接

Delphi webbroker 输出图片的问题

Delphi 限制edit输入 多个例子

Delphi中使用低层钩子 屏蔽win、ctrl+esc、alt+tab、alt+f4

Delphi 递归遍历 treeview树节点

Delphi tgifimage:timage显示gif动画

Delphi sysem.json 链式写法

Delphi xe5 android toast

Delphi将image存入mysql数据库

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...