使用PHP的SOAP扩展创建此SOAP XML请求

时间:2012-10-12 15:21:45

标签: php api soap

我正在尝试使用PHP SOAP extension重新创建此XML请求并且无法执行此操作:

<?xml version="1.0"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.web.stormpost.skylist.com">
  <soapenv:Header>
    <authInfo xmlns:soap="http://skylist.com/services/SoapRequestProcessor" xsi:type="soap:authentication">
      <!--You may enter the following 2 items in any order-->
      <username xsi:type="xsd:string">****@example.com</username>
      <password xsi:type="xsd:string">*******</password>
    </authInfo>
  </soapenv:Header>
  <soapenv:Body>
    <ser:doImportFromTemplate soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <impTempId xsi:type="xsd:int">7</impTempId>
      <data xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:string">
Joe|Doe|joe@example.com John|Doe|john@example.com </data>
    </ser:doImportFromTemplate>
  </soapenv:Body>
</soapenv:Envelope>

到目前为止,这是我的测试代码:

// set connection params
$wsdl = 'http://api.stormpost.datranmedia.com/services/SoapRequestProcessor?wsdl';
$namespace = 'https://api.stormpost.datranmedia.com/services/SoapRequestProcessor';
$credentials = (object)array(
    'username' => '****@example.com',
    'password' => '*******'
);
$auth_values = new SoapVar($credentials, SOAP_ENC_OBJECT);
$header =  new SoapHeader($namespace, "authInfo", $auth_values);

// set client, headers, and call function
$client = new SoapClient($wsdl, array('trace' => true));
$client->__setSoapHeaders(array($header));
$client->__soapCall("doImportFromTemplate", array(7, 'Joe|Doe|joe@example.com John|Doe|john@example.com'));

似乎没有正确地使用xsi:type="soap:authentication"实现 authInfo 节点。我应该如何更改我的代码以输出此XML?我找不到好的实施例子。

1 个答案:

答案 0 :(得分:1)

前一段时间我自己也在使用php SOAP客户端,但我并没有真正理解这一点,但这对我有用:

$auth_values = array('username' => '****@example.com', 'password' => '*******');
$header =  new SoapHeader($namespace, "authInfo", $auth_values, false);

// set client, headers, and call function
$client = new SoapClient($wsdl, array('trace' => true));
$client->__setSoapHeaders(array($header));