Einzelnen Beitrag anzeigen

gabneo

Registriert seit: 15. Okt 2006
Ort: Deutsche Toskana :)
93 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#7

AW: PHPArray über Indy HTTP Post übergeben?

  Alt 3. Jan 2020, 18:40
Vielleicht ist JSON in diesem Fall interessant - denn sowohl Delphi als auch PHP verstehen das einwandfrei.

Delphi-Dokumentation: http://docwiki.embarcadero.com/RADStudio/Rio/de/JSON
JSON zu PHP-Array in PHP umwandeln: https://www.php.net/manual/de/function.json-decode.php

Zudem empfehle ich das an den Server per POST zu übermitteln und nicht per URL/GET.

Hier mal etwas pseudo code:

Delphi-Quellcode:
uses JSON, JSON.Types, JSON.Writers, JSON.Builders, Wininet;

procedure post;
var
  inetSession, inetConnection, inetRequest: HINTERNET;
  js, jsElement: TJSONObject;
  JSarray: TJSONArray;
  Parameter:String
begin
  if inetSession = nil then inetSession := InternetOpen(PChar('Delphi App'), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if inetSession = nil then RaiseLastOSError;

  if inetConnection = nil then inetConnection := InternetConnect(inetSession, PChar('###URL z.B.: www.demo.de ###'), INTERNET_DEFAULT_HTTPS_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 1);
  if inetConnection = nil then RaiseLastOSError;

  inetRequest := HttpOpenRequest(inetConnection, PChar('POST'), nil, nil, nil, nil, INTERNET_FLAG_SECURE, 1);
  if inetRequest = nil then RaiseLastOSError;

  //JSON aufbauen
  js := TJSONObject.Create;
  JSarray := TJSONArray.Create;
  jsElement := TJSONObject.Create;
  try
    js.AddPair(TJSONPair.Create('Propertyname', 'ABC'));
    jsElement.AddPair(TJSONPair.Create('arrayElement', TJSONNumber.Create(10)));
    JSarray.AddElement(jsElement);
    js.AddPair(TJSONPair.Create('array', JSarray));
    Parameter := js.ToJSON;
  finally
    jsElement.free;
    JSarray.free;
    js.free;
  end;

  //JSON posten
  if not HttpAddRequestHeaders(inetRequest, PChar(Header), Length(Header), HTTP_ADDREQ_FLAG_ADD) then RaiseLastOSError;
  if not HTTPSendRequest(inetRequest, nil, 0, PAnsiChar(AnsiString(Utf8Encode(Parameter))), length(Utf8Encode(Parameter))) then RaiseLastOSError;
end;
Viele Grüße

Geändert von gabneo ( 3. Jan 2020 um 18:53 Uhr)
  Mit Zitat antworten Zitat