如何给soap添加header头 php

如何给soap添加header头 php,第1张

使用php 设置soapHeader时要如下进行,不然发出去的包会变成类似“<item><key>user</key><value>23107720</value></item>”的格式:  $auth_header = array( 'user'=>$key, 'password'=>$pwd ) // 下面的RequestSOAPHeader 对应 wsdl 定义里面的 <xsd:element name="RequestSOAPHeader">..... $authvalues = new SoapVar($auth_header, SOAP_ENC_OBJECT,"RequestSOAPHeader",$uri)$header = new SoapHeader($uri, 'RequestSOAPHeader', $authvalues)$api = new SoapClient(null,$options)$api->__setSoapHeaders(array($header))不明白的话可以去后盾人看看相关的教学视频。

在 Delphi 中添加 SOAP Header 就不用说了,请参考 Delphi 自带示例,位于 Demos/WebServices/SOAPHeaders 目录下。添加 Http header 字段示例如下。

示例 WSDL 文件描述如下:

SampleServiceSoap = class(IInvokable)

['{08323867-2307-4569-8405-4E575CC3C453}']

procedure SampleProcedure1

procedure SampleProcedure2

end

function GetSampleServiceSoap(UseWSDL: BooleanAddr: stringHTTPRIO: THTTPRIO): SampleSoap

const

defWSDL = 'http://127.0.0.1/SampleService.asmx?WSDL'

defURL = 'http://127.0.0.1/SampleService.asmx'

defSvc = 'SampleService'

defPrt = 'SampleServiceSoap'

var

RIO: THTTPRIO

begin

Result := nil

if (Addr = '') then

begin

if UseWSDL then

Addr := defWSDL

else

Addr := defURL

end

if HTTPRIO = nil then

RIO := THTTPRIO.Create(nil)

else

RIO := HTTPRIO

try

Result := (RIO as SampleServiceSoap)

if UseWSDL then

begin

RIO.WSDLLocation := Addr

RIO.Service := defSvc

RIO.Port := defPrt

end else

RIO.URL := Addr

finally

if (Result = nil) and (HTTPRIO = nil) then

RIO.Free

end

end

增加 Http header 示例如下:

unit Unit1

interface

uses

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

Dialogs, StdCtrls, SOAPHTTPTrans, SOAPHTTPClient, ActiveX

type

TForm1 = class(TForm)

Button1: TButton

procedure Button1Click(Sender: TObject)

private

procedure BeforePost(const HTTPReqResp: THTTPReqRespData: Pointer)

public

end

var

Form1: TForm1

implementation

{$R *.dfm}

// 在发送请求之前在 http header 中添加 CustomHeader 字段

// Data 是指向 HINTERNET 类型的指针

procedure TForm1.BeforePost(const HTTPReqResp: THTTPReqResp

Data: Pointer)

const

csCustomHeader = 'CustomHeader:XXXXXXXX'

begin

HttpAddRequestHeaders(Data, PChar(csCustomHeader), Length(csCustomHeader),

HTTP_ADDREQ_FLAG_ADD)

end

procedure TForm1.Button1Click(Sender: TObject)

var

Soap: SampleServiceSoap

RIO: THTTPRIO

begin

CoInitialize(nil)

RIO := THTTPRIO.Create(nil)

try

RIO.HTTPWebNode.OnBeforePost := BeforePost

Soap := GetSampleServiceSoap(False, 'http://127.0.0.1/SampleService.asmx', RIO)

if Assigned(Soap) then

Soap.SampleProcedure1

finally

RIO.Free

CoUninitialize

end

end

end.


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11394736.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存