略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: SoapHeader

2024-12-22

The SoapHeader class

(PHP 5, PHP 7, PHP 8)

简介

Represents a SOAP header.

类摘要

class SoapHeader {
/* 属性 */
public string $namespace;
public string $name;
public mixed $data = null;
public bool $mustUnderstand;
public string|int|null $actor;
/* 方法 */
__construct(
    string $namespace,
    string $name,
    mixed $data = ?,
    bool $mustunderstand = ?,
    string $actor = ?
)
}

属性

actor

data

mustUnderstand

name

namespace

目录

add a noteadd a note

User Contributed Notes 4 notes

up
16
john at jtresponse dot co dot uk
10 years ago
None of the examples really do it for me.
Note: you should NOT need to hard-code any XML.

Here is an example of creating a nested header and including a parameter.

$client = new SoapClient(WSDL,array());

$auth = array(
        'UserName'=>'USERNAME',
        'Password'=>'PASSWORD',
        'SystemId'=> array('_'=>'DATA','Param'=>'PARAM'),
        );
  $header = new SoapHeader('NAMESPACE','Auth',$auth,false);
  $client->__setSoapHeaders($header);

Gives the following header XML:

  <SOAP-ENV:Header>
    <ns1:Auth>
      <ns1:SystemId Param="PARAM">DATA</ns1:SystemId>
      <ns1:UserName>USERNAME</ns1:UserName>
      <ns1:Password>PASSWORD</ns1:Password>
    </ns1:Auth>
  </SOAP-ENV:Header>
up
2
ericvaneldik at gmail dot com
3 years ago
If you want to create an soap header wihtout namespace and without an item key value setup, you can use SoapVar

To get this:
<SOAP-ENV:Header>
    <IdentityHeader>
      <SessionID>123456789</SessionID>
    </IdentityHeader>
  </SOAP-ENV:Header>

you can use this php code:
<?php
$headerVar
= new SoapVar('<IdentityHeader><SessionID>123456789</SessionID></IdentityHeader>',
     
XSD_ANYXML);
$header = new SoapHeader('http://tempuri.org/','RequestParams',
     
$headerVar);
?>
up
3
abdul dot rashid at paytabs dot co
6 years ago
Just to add some note regarding his john at jtresponse dot co dot uk

In PHP you can try following code to avoid the <item><key/>

$Auth = new stdClass();
$Auth->SystemId = "DATA";
$Auth->UserName = "USERNAME";
$Auth->Password = "PASSWORD";

$header = new SoapHeader('NAMESPACE','Auth',$Auth,false);
$soapClient->__setSoapHeaders($header);

  <SOAP-ENV:Header>
    <ns1:Auth>
      <ns1:SystemId>DATA</ns1:SystemId>
      <ns1:UserName>USERNAME</ns1:UserName>
      <ns1:Password>PASSWORD</ns1:Password>
    </ns1:Auth>
  </SOAP-ENV:Header>
up
-2
voroks at logics dot net dot au
6 years ago
Example by john at jtresponse dot co dot uk does miss one important point: to be able to add attributes they must be mentioned in WSDL. If they not exist in WSDL they WILL NOT appear as attributes but rather <item><key/><value/></item> elements.

官方地址:https://www.php.net/manual/en/class.soapheader.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3