关于使用apache PHP作中间层Delphi接口程序3

3. Delphi  使用接口

FIDHttp:TIdHTTP;

const FBaseURL:string='apache 服务器地址';


procedure TWisePHPClient.ConnectSvr(Facc,FPwd:string);
begin
  FIDHttp.Request.Clear;
  FIDHttp.Request.BasicAuthentication:=True;
  FIDHttp.Request.Username:=Facc;
  FIDHttp.Request.Password:=FPwd;
end;

function TWisePHPClient.ExecultRestMethodByPHP(functionname:string;params:TJSONObject):TStringStream;
var RequestList : TStrings;
    ress:TStringStream;
begin
  FIDHttp.Request.ContentType:='text/html';
  RequestList:=TStringList.Create;
  if functionname<>'' then
    RequestList.Add('functionname='+functionname);
  if params<>nil then
    RequestList.Add('params='+TNetEncoding.Base64.Encode(params.ToJSON));
  ress := TStringStream.Create('',TEncoding.UTF8);

  FIDHttp.post(FBaseURL+'phpapi.php',RequestList,ress);

  if FIDHttp.ResponseCode<>200 then Sys.Dialogs.ShowError('連接服務器PHP API意外出錯')
  else begin
    Result:=ress;//TStringStream.Create(ress.DataString,TEncoding.UTF8);
  end;
  RequestList.Free;
end;


function GetDelphiFieldTypeFromPDO(pdofield:string):TFieldType;
var rjs,tmp:string;  //Reference Json string
    m,n,s:Integer;
begin
  rjs:='"int":3,"nvarchar":24,"binary":12,"bit":5,"char":1,'+
         '"date":9,"datetime":11,"datetime2":36,"datetimeoffset":36,'+
         '"decimal":37,"float":6,"geography":15,"geometry":15,'+
         '"udt":15,"image":15,"money":7,"nchar":24,'+
         '"ntext":39,"numeric":37,"real":51,"smalldatetime":11,'+
         '"smallint":2,"smallmoney":7,"sql_variant":24,"text":16,'+
         '"time":10,"timestamp":36,"tinyint":44,"uniqueidentifier":35,'+
         '"varbinary":13,"varchar":1,"xml":20,"bigint":25,'+
        '"LONGLONG":25,"STRING":1,"BIT":5,'+
        '"BLOB":1,"TINY":43,'+
        '"DATE":9,"DATETIME":11,'+
        '"NEWDECIMAL":8,"DOUBLE":6,"FLOAT":6,'+
        '"LONG":3,"INT24":3,"SHORT":2,"TIME":10,'+
        '"TIMESTAMP":36,"VAR_STRING":1,"YEAR":4';
  tmp:='"'+pdofield+'"';
  m:=Pos(tmp,rjs);
  s:=m+Length(tmp)+1;
  tmp:=Copy(rjs,s,2);
  if tmp[2]=',' then tmp:=Copy(tmp,1,1);
  n:=StrToIntDef(tmp,1);
  result:=TFieldType(n);
end;

procedure TWisePHPClient.GetDBSqlDataFromPHP(sql: string; DBName: string;IQry:TFDMemTable);
var pam:TJSONObject;
    jfields,jDataSet: TJSONArray;
    alldd: TJSONObject;
    i,j,w: Integer;
    ress,fdn,fdt,fdv:string;
    fdtt:TFieldType;
begin
  pam:=TJSONObject.Create;
  try
    pam.AddPair('dbname',DBName);
    pam.AddPair('sqlstr',sql);
    IQry.Active:=False;
    ress:=ExecultRestMethodByPHP('db_querytojson',pam).DataString;
    if ress<>'' then begin
      alldd:=TJSONObject.Create.ParseJSONValue(ress) as TJSONObject;
      jfields:=alldd.GetValue('field_data') as TJSONArray;
      jDataSet:=alldd.GetValue('row_data') as TJSONArray;
      IQry.FieldDefs.Clear;
      for I := 0 to jfields.size-1 do begin
        fdn:=TJSONObject(jfields.Get(i)).GetValue('fieldname').value;
        fdt:=TJSONObject(jfields.Get(i)).GetValue('fieldtype').Value;
        w:=StrToIntDef(TJSONObject(jfields.Get(i)).GetValue('len').Value,0);
        fdtt:=GetDelphiFieldTypeFromPDO(fdt);
        try
          IQry.FieldDefs.Add(fdn,fdtt,w);
        except
          IQry.FieldDefs.Add(fdn,fdtt,0);
        end;
      end;
      IQry.CreateDataSet;
      IQry.CachedUpdates:=True;
      for i := 0 to jDataSet.Size - 1 do  begin
        IQry.Append;
        for j := 0 to TJSONObject(jDataSet.Get(i)).Count - 1 do begin
          fdn:=TJSONObject(jDataSet.Get(i)).Pairs[j].JsonString.Value;
          fdv:=TJSONObject(jDataSet.Get(i)).Pairs[j].JsonValue.Value;
          try
            if fdv='null' then fdv:='' else
            if IQry.Fields[j].DataType in [ftDate, ftTime, ftDateTime] then
              IQry.Fields[j].Value:=StrToDateTime(fdv)
            else IQry.Fields[j].Value :=fdv ;
          except
            IQry.Fields[j].Value :=null ;
          end;
        end;
        IQry.Post;
      end;
    end;
  finally
    pam.Free;
    alldd.Free;
    IQry.Active:=True;
    IQry.First;
  end;
end;


function TWisePHPClient.HttpDownLoadFile(svrfile,tofile:string):Boolean;
var MyMemoryStream: TMemoryStream;
    downloadUrl,topath,lwfn,tmp: string;
begin

  lwfn:=Trim(LowerCase(svrfile));
  if (lwfn='') or (lwfn='*') then exit;
  downloadUrl:=FBaseURL+'phpapi.php?dl='+httpencode(svrfile);
  MyMemoryStream := TMemoryStream.Create;
  FIDHttp.Get(downloadUrl, MyMemoryStream);
  result:=FIDHttp.ResponseCode=200;
  if not result then Sys.Dialogs.ShowError('下載文件('+downloadUrl+')意外出錯')
  else begin
    if Pos('Error,File not found',FIDHttp.ResponseText)>0 then
      Sys.Dialogs.ShowError('文件('+downloadUrl+')沒找到,'+FIDHttp.ResponseText)
    else begin
      MyMemoryStream.SaveToFile(tofile);
    end;
  end;
end;

function TWisePHPClient.HttpUploadFile(localfile,tosvrpath:string):Boolean;
var postStream : TIdMultiPartFormDataStream;
    baseurl,ress:string;
begin
  postStream := TIdMultiPartFormDataStream.Create;
  postStream.AddFormField('uploadtopath', tosvrpath); // 表单参数
  postStream.AddFile('wisefile', localfile); // 表单文件
  FIDHttp.Request.ContentType:='multipart/form-data';
  baseurl:=FBaseURL+'phpapi.php';
  ress:=FIDHttp.Post(baseurl,postStream);
  result:=FIDHttp.ResponseCode=200;
  if not result then
    Sys.Dialogs.ShowError('上傳文件('+localfile+')意外出錯:'+ress)
  else begin
    if Pos('Error',ress)>0 then
      Sys.Dialogs.ShowError('上傳文件('+localfile+')文件失敗:'+ress);
  end;
  postStream.Free;
end;

function TWisePHPClient.ExecSql(Sqlstr: string; DBName: string): integer;
var pam:TJSONObject;
begin
   pam:=TJSONObject.Create;
  try
    pam.AddPair('dbname',DBName);
    pam.AddPair('sqlstr',Sqlstr);
    Result:=strtointdef(ExecultRestMethodByPHP('db_execsql',pam).DataString,0);
  finally
    pam.Free;
  end;
end;


function TWisePHPClient.BathExecuteSqls(sqls:TStrings; DBName: string):Integer;
var i:Integer;
    ssql,rss:string;
    pam:TJSONObject;
begin
  for I := 0 to sqls.count-1 do
    if ssql='' then ssql:=sqls.strings[i]
    else ssql:=ssql+'|'+sqls.strings[i];
  pam:=TJSONObject.Create;
  try
    pam.AddPair('dbname',DBName);
    pam.AddPair('sqlstr',ssql);
    rss:=ExecultRestMethodByPHP('db_batchexecsql',pam).DataString;
    result:=strtointdef(rss,0);
  finally
    pam.Free;
  end;
end;


procedure TWisePHPClient.MyQuery(IQry: TFDMemTable; ISqlStr: string;DBname:string);
begin
  GetDBSqlDataFromPHP(ISqlStr,DBname,IQry);
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我们都知道,Bolrand 从Delphi 6.0 以后开始支持Apache web 服务器,而Apache 以其出色的性能及安全性是很多人青睐。Apache 现在最新的版本为2.0.43。虽然Borland 今年新出的Delphi 7 开始支持Apache 2.0 版本,但是Delphi 6 只支持Apache 1.x 版本,而Delphi 7 发行的版本也只支持到Apache 2.0.39 版,对Apache 2.0.40 以后的版本无法支持,本文给出了让Delphi 6和Delphi7 支持最新的Apache 2.0.43 的方法。首先让我们对Delphi 7 进行改造,Delphi 7.0 不支持 Apache 2.0.40 以后的版本的原因是Apache 2.0.40 以后版本修改了接口文件,因此要对Delphi 7.0 对应的接口文件进行修改,具体的修改文件为: HTTPD2.pas,修改内容为:打开 HTTPD2.pas (在c: Program Filesorlanddelphi7source Internet下) 修改一下常数: · MODULE_MAGIC_NUMBER_MAJOR = 20020628; { Apache 2.0.40 }· 如果是Apache 2.0.43 的话,应该修改为· MODULE_MAGIC_NUMBER_MAJOR = 20020903; { Apache 2.0.43 } MODULE_MAGIC_NUMBER_MINOR = 0; (* 0...n *)在结构 conn_rec 的定义里加入以下: · ap_conn_keepalive_e = (AP_CONN_UNKNOWN, AP_CONN_CLOSE, AP_CONN_KEEPALIVE);在结构 conn_rec 的定义里替换: · (** Are we still talking? *)· flags: Cardinal;· { The following are in the flags bitset:· unsigned aborted:1;· · (** Are we going to keep the connection alive for another request?· * -1 fatal error, 0 undecided, 1 yes *)· signed int keepalive:2;· · (** have we done double-reverse DNS? -1 yes/failure, 0 not yet,· * 1 yes/success *)· signed int double_reverse:2;· }为: (** Are we still talking? *) flags1: Cardinal; { The following are in the flags bitset: unsigned aborted:1; } (** Are we going to keep the connection alive for another request? * @see ap_conn_keepalive_e *) keepalive: ap_conn_keepalive_e; flags2: Cardinal; { The following are in the flags bitset: (** have we done double-reverse DNS? -1 yes/failure, 0 not yet, * 1 yes/success *) signed int double_reverse:2; }好了,保存这个文件,然后拷入c: Program Filesorlanddelphi7lib 目录。重新编译你的程序,加入相应的Apache 的配置(具体配置方法请参照我以前的文章),启动Apache,打开浏览器。 ok! 没问题了吧!好。现在我们看看怎么在Delphi 6 里面实现Apache 2.0 的支持,实际上很简单,只要把Delphi 7 里面相应的文件拷入 Delphi 6 的LIB 目录就可以了,具体为以下三个文件: ApacheTwoHTTP.pas, ApacheTwoApp.pas, HTTPD2.pas,然后打开你的现有的Apache 1.x 的程序,修改project 上面的 use 部分,把 Apacheapp改为Apachetwoapp,再把下面的ContentType 改为handler,好了,所有手术完成,现在你编译出来的就是支持Apache 2.0.43 的动态共享模块了。注意:以上的修改为非官方修改,不能保证不出问题,请大家慎重处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值