如何使用PHP创建任意SOAP请求?

时间:2010-04-16 14:14:45

标签: php soap wsdl client

我在PHP中遇到了与SOAP相关的问题。我正在尝试使用Nusoap_client类创建任意SOAP请求。包含标题的完整请求应如下所示。当然占位符(字符串)应该用实际值替换。

POST /services/RecipeCouponAPI11.asmx HTTP/1.1
Host: example.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.example.com/services/GetCouponAll"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <RCAuthenticator xmlns="http://www.example.com/services/">
      <UserName>string</UserName>
      <Password>string</Password>
    </RCAuthenticator>
  </soap:Header>
  <soap:Body>
    <GetCouponAll xmlns="http://www.example.com/services/">
      <campaignCode>string</campaignCode>
    </GetCouponAll>
  </soap:Body>
</soap:Envelope>

我尝试了以下PHP代码而没有运气。好像请求格式不正确。关于如何让他工作的任何想法?

<?php
$client = new Nusoap_client('http://www.example.com/services/RecipeCouponAPI11.asmx');
$headers = new SoapHeader(
    'http://www.example.com/services/',
    'RCAuthenticator',
    (object)array(
        'UserName' => 'username',
        'Password' => 'password'
    ),
    false
);

$client->setHeaders(array($headers));

$response = $client->call(
    'GetCouponAll',
    array('campaignCode' => 'campaigncode'),
    null,
    'http://www.example.com/services/GetCouponAll'
);
?>

1 个答案:

答案 0 :(得分:3)

毕竟找到了解决方案。

<?php
$client = new SoapClient('http://www.example.com/services/RecipeCouponAPI11.asmx?wsdl');
$header = new SoapHeader(
    'http://www.example.com/services/',
    'RCAuthenticator',
    array(
        'UserName' => 'username',
        'Password' => 'password'
    )
);

$client->__setSoapHeaders($header);

$response = $client->GetCouponAll(array('campaignCode' => ''));
?>